使用HttpClient对Web Service调用
用到的jar包有:
commons-codec-1.3.jar
commons-httpclient-3.0.jar
commons-logging-1.0.4.jar
具体流程大致这样:
String soapRequestData = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">" +
"<soap12:Body>" +
" <qqCheckOnline xmlns=\"http://WebXml.com.cn/\">" +
" <qqCode>349104641</qqCode>" +
" </qqCheckOnline>" +
" </soap12:Body>" +
"</soap12:Envelope>";
System.out.println(soapRequestData);
//PostMethod postMethod = new PostMethod("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl");
PostMethod postMethod = new PostMethod("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl");
// 然后把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();
int statusCode = httpClient.executeMethod(postMethod);
String soapResponseData = postMethod.getResponseBodyAsString();
System.out.print(soapResponseData);
动态构造调用串的话如下实现:
private String tns;
private String methodName;
private String wsdlLocation;
private String soapResponseData;
private int invoke(Map<String, String> patameterMap) throws Exception {
PostMethod postMethod = new PostMethod(wsdlLocation);
String soapRequestData = buildRequestData(patameterMap);
byte[] bytes = soapRequestData.getBytes("utf-8");
InputStream inputStream = new ByteArrayInputStream(bytes, 0,
bytes.length);
RequestEntity requestEntity = new InputStreamRequestEntity(inputStream,
bytes.length, "application/soap+xml; charset=utf-8");
postMethod.setRequestEntity(requestEntity);
HttpClient httpClient = new HttpClient();
int statusCode = httpClient.executeMethod(postMethod);
soapResponseData = postMethod.getResponseBodyAsString();
return statusCode;
}
private String buildRequestData(Map<String, String> patameterMap) {
StringBuffer soapRequestData = new StringBuffer();
soapRequestData.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
soapRequestData
.append("<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">");
soapRequestData.append("<soap12:Body>");
soapRequestData.append("<" + methodName + " xmlns=\"" + tns + "\">");
Set<String> nameSet = patameterMap.keySet();
for (String name : nameSet) {
soapRequestData.append("<" + name + ">" + patameterMap.get(name)
+ "</" + name + ">");
}
soapRequestData.append("</" + methodName + ">");
soapRequestData.append("</soap12:Body>");
soapRequestData.append("</soap12:Envelope>");
return soapRequestData.toString();
}
分享到:
相关推荐
httpclient调用webservice.txt
一个使用HttpClient访问WS 的例子,包含相关jar包
希望用http协议的能可以帮到大家!很有使用价值!大家可以看看!
ETL KETTLE 中利用Httpclient 调用webservice接口获取XML数据,并解析XML 数据。 完整的KTR实例
commons-codec-1.4.jar、commons-httpclient-3.0.1.jar、commons-logging-1.1.1.jar
webservice调用实例,通过HttpClient调用,包括完整参数调用,项目通过maven搭建
程序首次访问服务时,都需要将服务转成动态库,这得花费一些时间,如果根据以上的做法,便能节约这里所花费的时间,从而提高了WebService的访问速度,虽然可能只能提速1秒,但已很可贵了。
WebService服务端会提供wsdl文件,客户端通过该文件生成.cs文件以及生成.dll. 注意:若服务端只提供的了URL,在URL后面加上”?wsdl”在浏览器上访问,复制页面内容,粘贴到文本文件,将文件后缀改为为”wsdl”,即可...
用来动态调用web service
相信大家在调用rest webservice通常使用httpclient.该示例代码是使用httpconnection的形式,以POST和GET两种方式提交调用REST WebService。在项目中一直使用的工具类,分享给大家
java httpclient调用webservice接口的post方法。 包含两种调用方式。
java采用httpclient和xfire两种方式调用webservice,此示例是调用的一个短信接口.
WebService调用的几种方式,总结了Axis,Cxf,HttpClient,Myeclipse反向生成,XFire调用方式。
NULL 博文链接:https://aperise.iteye.com/blog/2223454
WebService客户端调用WebService服务示例代码,java代码,纯手工,包括直接httpClient直接发送Saop报文调用和利用wsimport -keep 生成客户端代码后调用两种方式的示例。因涉及商务账户密码隐私,因此代码中的有些账号...
研究Webservice异步调用的实现, 1、通过异步调用的方式实现高性能的使用WebService的API
java调用webservice的jar包包含 commons-httpclient-3.1.jar, commons-codec-1.3.jar, commons-logging.jar这3个jar包
http_wcf调用 解决方案,用到了webService和 http_wcf调用 解决方案,用到了webService和http_wcf调用 解决方案,用到了webService和
这个自己写的xFire发布WebService,包含发布和调用,可以用在企业中,而且所用的jar也比较少,不会出现很多jar的问题,一共才9个jar,[commons-codec-1.3.jar,commons-httpclient-3.0.jar,commons-logging-1.0.4.jar,...
NULL 博文链接:https://leon1509.iteye.com/blog/1541387