如何使用Java使用rpc编码的SOAP Web服务 [英] How to consume rpc-encoded SOAP Web Service with Java

查看:98
本文介绍了如何使用Java使用rpc编码的SOAP Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

is there a way to consume a SOAP web service with java by just using:

  • 所需的SOAPaction(例如称为"find"的方法名)
  • 网络服务的网址
  • 标头身份验证(用户名和密码)
  • 最后输出结果

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

I have an example request xml file by successfully consuming it with php but I can't find a proper way to do it on java.

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

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

推荐答案

您可以使用

You can use java.net.HttpURLConnection to send SOAP messages. e.g.:

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();

}

这篇关于如何使用Java使用rpc编码的SOAP Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆