java - 在java中调用Web服务SOAP

标签 java web-services soap

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

确保你的 xmlsoap 在那里工作......

发送之前您的 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>

关于java - 在java中调用Web服务SOAP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22868791/

相关文章:

c# - 使用 WCF 从 .svc 创建 .cs 文件的正确方法

java - 一个线程中的 Thread.sleep() 使 UI 线程也进入休眠状态

java - 如何在 Hortonworks VM 上的 Spark 上运行 .jar?

java - 更改 URL 模式,欢迎文件显示 404

c# - 组合 URI 和路径

node.js - NODE-SOAP 错误处理

java - 登录tomcat不起作用

java - 正则表达式 - 用换行符替换标签之间的文本

java - 如何将 Spring Boot Web 服务抛出的异常映射到复杂的故障信息?

javascript - 从 Javascript 连接到 SOAP Web 服务?