簡體   English   中英

用Java調用Web服務SOAP

[英]Calling a web service SOAP in java

我想在鏈接中調用以下Web服務:
http://www.webservicex.net/globalweather.asmx?op=GetCitiesByCountry

這是主程序:

public static void main(String[] args) {
    try {
        // Create the connection
        SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
        SOAPConnection conn = scf.createConnection();

        // Create message
        MessageFactory mf = MessageFactory.newInstance();
        SOAPMessage msg = mf.createMessage();

        // Add eventually a SoapAction header if necessary

        MimeHeaders hd = msg.getMimeHeaders(); hd.addHeader("SOAPAction", "http://www.webserviceX.NET/GetCitiesByCountry");

        // Object for message parts
        SOAPPart sp = msg.getSOAPPart();

        SOAPEnvelope env = sp.getEnvelope();

        SOAPBody bd = env.getBody();

        // Populate body
        // Main element and namespace
        SOAPElement be = bd.addChildElement(env.createName("GetCitiesByCountry", "ansi", "http://www.webserviceX.NET"));

        // Add content
        be.addChildElement("CountryName").addTextNode("Morocco");

        // Save message
        msg.saveChanges();

        // View input
        System.out.println("\n Soap request:\n");
        msg.writeTo(System.out);
        System.out.println();

        // Send
        String urlval = "http://www.webservicex.net/globalweather.asmx";
        // or /rcx-ws-rpc/rcx for my rpc/encoded web service

        SOAPMessage rp = conn.call(msg, urlval);

        // View the output
        System.out.println("\nXML response\n");

        // Create transformer
        TransformerFactory tff = TransformerFactory.newInstance();
        Transformer tf = tff.newTransformer();

        // Get reply content
        Source sc = rp.getSOAPPart().getContent();

        // Set output transformation
        StreamResult result = new StreamResult(System.out);
        tf.transform(sc, result);
        System.out.println();

        // Close connection
        conn.close();

    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

我收到以下錯誤響應:

<?xml version="1.0" encoding="UTF-8"?>
  <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <soap:Fault>
      <faultcode>soap:Server</faultcode>
      <faultstring>
        System.Web.Services.Protocols.SoapException:
          Server was unable to process request. ---&gt;
          System.Data.SqlClient.SqlException: Procedure or function 'getWCity'
          expects parameter '@CountryName', which was not supplied.
        at WebServicex.GlobalWeather.GetCitiesByCountry(String CountryName)   
        --- End of inner exception stack trace ---
      </faultstring><detail/>
    </soap:Fault>
  </soap:Body>
</soap:Envelope>

有人有答案嗎?

在圍繞程序包裝程序之前,我總是使用SOAPUI來測試我的Web服務調用。 http://www.soapui.org/

在您的情況下,wsdl是http://www.webservicex.net/globalweather.asmx?WSDL

確保您的xml soap在那里工作...

您的xml文件在發送之前是什么?

應該是這樣的

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webserviceX.NET">
   <soapenv:Header/>
   <soapenv:Body>
      <web:GetCitiesByCountry>             
         <web:CountryName>Africa</web:CountryName>
      </web:GetCitiesByCountry>
   </soapenv:Body>
</soapenv:Envelope>

我正在刪除上下文“ ansi”及其工作

    // Main element and namespace
    SOAPElement be = bd.addChildElement(env.createName("GetCitiesByCountry", "", "http://www.webserviceX.NET"));

謝謝你們

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM