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

jsu*_*urf 0 java soap soap-rpc-encoded

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

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

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

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

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

Pau*_*gas 5

您可以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();

}
Run Code Online (Sandbox Code Playgroud)