【问题标题】:Calling a web service SOAP in java在 Java 中调用 Web 服务 SOAP
【发布时间】:2014-04-04 17:02:09
【问题描述】:

我想在链接中调用以下网络服务:
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>

有人有答案吗?

【问题讨论】:

  • 您应该从 WSDL 开始 - "webservicex.net/globalweather.asmx?WSDL"
  • 这是发送前的 xml 文件:&lt;SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"&gt;&lt;SOAP-ENV:Header/&gt;&lt;SOAP-ENV:Body&gt;&lt;ansi:GetCitiesByCountry xmlns:ansi="http://www.webserviceX.NET"&gt;&lt;CountryName&gt;Morocco&lt;/CountryName&gt;&lt;/ansi:GetCitiesByCountry&gt;&lt;/SOAP-ENV:Body&gt;&lt;/SOAP-ENV:Envelope&gt;

标签: java web-services soap


【解决方案1】:

我总是使用 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>

【讨论】:

    【解决方案2】:

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

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

    谢谢大家

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-03
      • 2012-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多