繁体   English   中英

如何使用Java使用rpc编码的SOAP Web服务

[英]How to consume rpc-encoded SOAP Web Service with Java

有没有一种方法可以通过仅使用以下方式使用java使用SOAP Web服务:

  • 所需的SOAPaction(例如,名为“ find”的方法名)
  • 网络服务的URL
  • 标头身份验证(用户名和密码)
  • 最终输出结果

我有一个示例示例xml文件,可以通过使用php成功使用它来获取,但是我找不到在Java上执行该请求的正确方法。

[更新:Web服务的WSDL样式是RPC /编码的]

[更新#2:您可以找到以下解决问题的方法(通过使用IDE生成的Java存根)]

您可以使用java.net.HttpURLConnection发送SOAP消息。 例如:

public static void main(String[] args) throws Exception {

    String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" +
            "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\n" +
            "  <soap:Body>\r\n" +
            "    <ConversionRate xmlns=\"http://www.webserviceX.NET/\">\r\n" +
            "      <FromCurrency>USD</FromCurrency>\r\n" +
            "      <ToCurrency>CNY</ToCurrency>\r\n" +
            "    </ConversionRate>\r\n" +
            "  </soap:Body>\r\n" +
            "</soap:Envelope>";

    Authenticator.setDefault(new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("username", "password".toCharArray());
        }
    });

    URL url = new URL("http://www.webservicex.net/CurrencyConvertor.asmx");
    URLConnection  conn =  url.openConnection();
    conn.setDoOutput(true);
    conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
    conn.setRequestProperty("SOAPAction", "http://www.webserviceX.NET/ConversionRate");

    // Send the request XML
    OutputStream outputStream = conn.getOutputStream();
    outputStream.write(xml.getBytes());
    outputStream.close();

    // Read the response XML
    InputStream inputStream = conn.getInputStream();
    Scanner sc = new Scanner(inputStream, "UTF-8");
    sc.useDelimiter("\\A");
    if (sc.hasNext()) {
        System.out.print(sc.next());
    }
    sc.close();
    inputStream.close();

}

经过长时间的搜索,我终于找到了一种使用rpc /编码的SOAP Web服务的方法。 我决定从wsdl网址生成客户端存根。

成功的方法是通过此链接(来源: 从RPC编码的WSDL生成Java客户端的最简单方法是什么

在通过eclipse / netbeans调整(java存根)的生成代码后,您可以简单地构建客户端。 通过使用其生成的类,可以使用您喜欢的soap api。

例如

Auth auth = new Auth("username", "password");
SearchQuery fsq = new SearchQuery ("param1","param2","param3");
Model_SearchService service = new Model_SearchServiceLoc();
SearchRequest freq = new SearchRequest(auth, fsq);
Result r[] = service.getSearchPort().method(freq);
for(int i=0; i<r.length; i++){
    System.out.println(i+" "+r[i].getInfo()[0].toString());
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM