繁体   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