gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 08:00:22 25 4
gpt4 key购买 nike

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

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

我有一个示例请求 xml 文件,通过 php 成功使用它,但我找不到在 java 上执行此操作的正确方法。

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

[更新 #2:您可以找到我如何解决下面的问题(通过使用 IDE 生成的 java stub )]

最佳答案

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

}

关于java - 如何使用 Java 使用 rpc 编码的 SOAP Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25768181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com