我们将Web Service发布在Tomcat或者其他应用服务器上后,有很多方法可以调用该Web Service,常用的有两种:
1、通过浏览器HTTP调用,返回规范的XML文件内容
2、通过客户端程序调用,返回结果可自定义格式
第一步:新建Java Project,项目名称为HttpCallWebService
第二步:将所需jar包导入到库中
第三步:编写调用class,这里有两种方式调用,即GET方式和POST方式,由于POST方式较安全,故这里采用POST方式调用;请求数据的构造也有两种方式:静态和动态构造,下面分别介绍这两种方式:
注:这里以E邮宝开放的webservice接口为例调用其中一个API函数,而E邮宝的webservice基于SOAP,故请求数据为SOAP格式,大家可根据自己情况进行修改
静态构造请求数据:
package com.http;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
public class StaticHttpclientCall {
/**
* @param args
* @throws IOException
* @throws HttpException
*/
public static void main(String[] args) throws HttpException, IOException {
// TODO Auto-generated method stub
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>"
+ " <GetAPACShippingPackage xmlns=\"http://shippingapi.ebay.cn/\">"
+ " <GetAPACShippingPackageRequest>"
+ " <TrackCode>123</TrackCode>"
+ " <Version>123</Version>"
+ " <APIDevUserID>123</APIDevUserID>"
+ " <APIPassword>123</APIPassword>"
+ " <APISellerUserID>123</APISellerUserID>"
+ " <MessageID>123</MessageID>"
+ " </GetAPACShippingPackageRequest>"
+ " </GetAPACShippingPackage>" + "</soap12:Body>"
+ " </soap12:Envelope>";
System.out.println(soapRequestData);
PostMethod postMethod = new PostMethod(
"http://epacketws.pushauction.net/v3/orderservice.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);
if(statusCode == 200) {
System.out.println("调用成功!");
String soapResponseData = postMethod.getResponseBodyAsString();
System.out.println(soapResponseData);
}
else {
System.out.println("调用失败!错误码:" + statusCode);
}
}
}
动态构造数据:
package com.http;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
// 动态构造调用串,灵活性更大
public class DynamicHttpclientCall {
private String namespace;
private String methodName;
private String wsdlLocation;
private String soapResponseData;
public DynamicHttpclientCall(String namespace, String methodName,
String wsdlLocation) {
this.namespace = namespace;
this.methodName = methodName;
this.wsdlLocation = wsdlLocation;
}
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=\"" + namespace
+ "\">");
soapRequestData.append("<" + methodName + "Request>");
Set<String> nameSet = patameterMap.keySet();
for (String name : nameSet) {
soapRequestData.append("<" + name + ">" + patameterMap.get(name)
+ "</" + name + ">");
}
soapRequestData.append("</" + methodName + "Request>");
soapRequestData.append("</" + methodName + ">");
soapRequestData.append("</soap12:Body>");
soapRequestData.append("</soap12:Envelope>");
return soapRequestData.toString();
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
DynamicHttpclientCall dynamicHttpclientCall = new DynamicHttpclientCall(
"http://shippingapi.ebay.cn/", "GetAPACShippingPackage",
"http://epacketws.pushauction.net/v3/orderservice.asmx?wsdl");
Map<String, String> patameterMap = new HashMap<String, String>();
patameterMap.put("TrackCode", "123");
patameterMap.put("Version", "123");
patameterMap.put("APIDevUserID", "123");
patameterMap.put("APIPassword", "123");
patameterMap.put("APISellerUserID", "123");
patameterMap.put("MessageID", "123");
patameterMap.put("TrackCode", "123");
String soapRequestData = dynamicHttpclientCall.buildRequestData(patameterMap);
System.out.println(soapRequestData);
int statusCode = dynamicHttpclientCall.invoke(patameterMap);
if(statusCode == 200) {
System.out.println("调用成功!");
System.out.println(dynamicHttpclientCall.soapResponseData);
}
else {
System.out.println("调用失败!错误码:" + statusCode);
}
}
}
最终运行结果:
可见最终返回的也是xml格式的数据,这里数据未进行格式化显示和处理
原文地址:http://www.cnblogs.com/lanxuezaipiao/archive/2013/05/10/3071584.html