gpt4 book ai didi

java - 将 POST xml 发送到 SOAP Web 服务不起作用

转载 作者:行者123 更新时间:2023-12-01 19:57:51 25 4
gpt4 key购买 nike

我正在尝试通过 java 应用程序将简单的 XML 发送到此 SOAP Web 服务: http://www.webservicex.net/geoipservice.asmx?op=GetGeoIP

我的代码目前是这样的:

        String url = "http://www.webservicex.net"; 
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);

post.setHeader("Host", "www.webservicex.net");
post.setHeader("Content-Type", "text/xml;charset=utf-8");
post.setHeader("SOAPAction", "http://www.webservicex.net/GetGeoIP");

String xmlString = "<?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" +
" <GetGeoIP xmlns=\"http://www.webservicex.net/\">\r\n" +
" <IPAddress>50.207.31.216</IPAddress>\r\n" +
" </GetGeoIP>\r\n" +
" </soap:Body>\r\n" +
"</soap:Envelope>";

List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("xml", xmlString));

post.setEntity(new UrlEncodedFormEntity(urlParameters));
HttpResponse response;
try {
response = client.execute(post);
System.out.println("Response Code : " +
response.getStatusLine().getStatusCode());

BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));

StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());

} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

我没有使用 WSLD 生成的类,正如您所看到的,我正在尝试直接发送 XML。但我似乎无法通过这种方式得到正确的响应,它只返回 302 或 400。

我是使用 SOAP 服务的初学者,我真的不知道我是否以正确的方式完成这一切。

谁能帮我解决这个问题吗?

<小时/>

更新

当我尝试通过 Advanced Rest Client 发出请求时:

Host: www.webservicex.net
Content-Type: application/xml
Content-Length: 362
SOAPAction: http://www.webservicex.net/GetGeoIP
<?xml version="1.0" encoding="utf-8"?>
<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/">
<soap:Body>
<GetGeoIP xmlns="http://www.webservicex.net/">
<IPAddress>string</IPAddress>
</GetGeoIP>
</soap:Body>
</soap:Envelope>

我得到:HTTP 错误 400。请求的 header 名称无效

最佳答案

首先将网址从“http://www.webservicex.net ”更改为“http://www.webservicex.net/geoipservice.asmx ”。

其次,将字符串添加为字符串实体解决了问题。

 StringEntity  xmlString = new StringEntity( "<?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" +
" <GetGeoIP xmlns=\"http://www.webservicex.net/\">\r\n" +
" <IPAddress>50.207.31.216</IPAddress>\r\n" +
" </GetGeoIP>\r\n" +
" </soap:Body>\r\n" +
"</soap:Envelope>");
post.setEntity(xmlString);

关于java - 将 POST xml 发送到 SOAP Web 服务不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48853623/

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