2014-02-20 43 views
-2

我在做一個項目,它必須訪問給定的Web服務並將數據發送到請求的參數並獲得服務響應。目前,我做了一些我從論壇中發現的東西,它將數據解析爲服務,但自從發出錯誤後無法獲得響應。 我的示例代碼如下。訪問Java Web服務並獲取響應

package webserviceexample; 
import javax.xml.soap.*; 
import javax.xml.transform.*; 
import javax.xml.transform.stream.*; 
public class aaaa { 
public static void main(String args[]) { 
    try {   
     SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); 
     SOAPConnection soapConnection = soapConnectionFactory.createConnection(); 
     String url = "https://vlmt.XXX.lk:0111/VendorRecharge/services/RechargeVendorService/wsdl/"; 
     SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url); 
     printSOAPResponse(soapResponse); 
     //soapConnection.close(); 
    } catch (Exception e) { 
     System.err.println("Error occurred while sending SOAP Request to Server"); 
     e.printStackTrace(); 
    } 
} 
private static SOAPMessage createSOAPRequest() throws Exception { 
    MessageFactory messageFactory = MessageFactory.newInstance(); 
    SOAPMessage soapMessage = messageFactory.createMessage(); 
    SOAPPart soapPart = soapMessage.getSOAPPart(); 
    String serverURI = "http://vendor.XXX.ZZZ.com/"; 

    SOAPEnvelope envelope = soapPart.getEnvelope(); 
    envelope.addNamespaceDeclaration("wsdl", serverURI); 

    SOAPBody soapBody1 = envelope.getBody(); 
    SOAPElement soapBodyElemX = soapBody1.addChildElement("performRecharge", "wsdl"); 

    SOAPElement soapBodyElem1 = soapBodyElemX.addChildElement("storeId", "wsdl"); 
    soapBodyElem1.addTextNode("LA19BNPX"); 
    SOAPElement soapBodyElem2 = soapBodyElemX.addChildElement("password", "wsdl"); 
    soapBodyElem2.addTextNode("[email protected]"); 
    SOAPElement soapBodyElem3 = soapBodyElemX.addChildElement("mobileNo", "wsdl"); 
    soapBodyElem3.addTextNode("751238456"); 
    SOAPElement soapBodyElem4 = soapBodyElemX.addChildElement("Amount", "wsdl"); 
    soapBodyElem4.addTextNode("12"); 
    SOAPElement soapBodyElem5 = soapBodyElemX.addChildElement("vendorTransactionId", "wsdl"); 
    soapBodyElem5.addTextNode("15"); 

    MimeHeaders headers = soapMessage.getMimeHeaders(); 
    headers.addHeader("performRecharge", serverURI + "performRecharge"); 
    System.out.println(""); 
    soapMessage.saveChanges(); 

    System.out.print("Request SOAP Message = "); 
    soapMessage.writeTo(System.out); 
    System.out.println(); 
    return soapMessage; 
} 

private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception { 
    TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
    Transformer transformer = transformerFactory.newTransformer(); 
    Source sourceContent = soapResponse.getSOAPPart().getContent(); 
    System.out.print("\nResponse SOAP Message = "); 
    StreamResult result = new StreamResult(System.out); 
    transformer.transform(sourceContent, result); 
} 

}

我得到的錯誤是:

請求SOAP消息

<SOAP-ENV:Envelope xmlns:SOAP- ENV="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:wsdl="http://vendor.virtualization.ibm.com/"> 
    <SOAP-ENV:Header/> 
    <SOAP-ENV:Body> 
    <wsdl:performRecharge> 
     <wsdl:storeId>LA19BNPX</wsdl:storeId> 
     <wsdl:password>[email protected]</wsdl:password> 
     <wsdl:mobileNo>751238456</wsdl:mobileNo> 
     <wsdl:Amount>12</wsdl:Amount> 
    <wsdl:vendorTransactionId>15</wsdl:vendorTransactionId> 
    </wsdl:performRecharge> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

響應(錯誤)

<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope 
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"  
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <soapenv:Header/> 
    <soapenv:Body> 
    <soapenv:Fault xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
     <faultcode>soapenv:Server.generalException</faultcode> 
     <faultstring>javax.xml.rpc.JAXRPCException: WSWS3122E: Error: Could not find service services/RechargeVendorService/wsdl/ referenced in URI /VendorRecharge/services/RechargeVendorService/wsdl/</faultstring> 
    </soapenv:Fault> 
    </soapenv:Body> 
</soapenv:Envelope> 

我必須在Web服務中調用「performRecharge」。請幫幫我。 Web服務的摘要如下。

請幫我解決這個問題。這很重要。 謝謝。

WSDL文件

+2

小寫請! – nachokk

+3

首先不要對我們大喊大叫。 – chrylis

回答

1

請確保這是正確的服務URL。

好像你指定了wsdl位置而沒有註冊服務url。設置該服務的wsdl文件中指定的請求url。

String url = "https://vlmt.XXX.lk:0111/VendorRecharge/services/RechargeVendorService/wsdl/"; 

查看位置標籤在您的WSDL

<service name="Hello_Service"> 
     <documentation>WSDL File for HelloService</documentation> 
     <port binding="tns:Hello_Binding" name="Hello_Port"> 
     <soap:address 
      location="**http://www.examples.com/SayHello/**"> 
     </port> 
    </service> 
+0

當我把URL放在瀏覽器中後,我可以在其中獲得performRecharge。 – maXfenda

+0

如果url正確,請嘗試使用http而不是https並查看它是否有效。 –

+0

我試過花花公子,但它給人巨大的例外 「2014年2月20日上午09點32分22秒com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection後 重度:SAAJ0009:消息發送失敗 將SOAP請求發送到服務器時發生錯誤 com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl:java.security.PrivilegedActionException:com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl:消息發送失敗「 – maXfenda