java调用webservice接口具体怎么调用

2024-12-18 16:42:23
推荐回答(1个)
回答1:

使用HttpClient
用到的jar文件:commons-httpclient-3.1.jar
方法:
预先定义好Soap请求数据,可以借助于XMLSpy Professional软件来做这一步生成。

String soapRequestData = "" +
"" +
"" +
" " +
" 219.137.167.157" +
"
" +
"
" +
"
";

然后定义一个PostMethod,这时需要指定web服务的Url;

PostMethod postMethod = new PostMethod(“http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx”);

然后把Soap请求数据添加到PostMethod中

byte[] b = soapRequestData.getBytes("utf-8");
InputStream is = new ByteArrayInputStream(b,0,b.length);
RequestEntity re = new InputStreamRequestEntity(is,b.length,"application/soap+xml; charset=utf-8");
postMethod.setRequestEntity(re);

最后生成一个HttpClient对象,并发出postMethod请求

HttpClient httpClient = new HttpClient();
statusCode = httpClient.executeMethod(postMethod);
String soapRequestData = postMethod.getResponseBodyAsString();

soapRequestData就是调用web服务的Soap响应数据,是xml格式的,可以通过解析soapRequestData来获得调用web服务的返回值。