HttpClient调用webservice

2010年07月6日  |  9:31 下午分类:WebService  |  365 views

webservice调用总结

WebService以其不可比拟的优越性,在实际应用中,深受开发人员的欢迎。以前实验室的项目多是采用AXIS2作为webservice的解决方案,虽然其作为服务器端性能不错,但是其作为客户端存在稍显笨重,不易开发人员迅速上手的问题。接触了一段时间的HttpClient,于是最近尝试了一下通过httpclient,进行webservice客户端请求的调用,整个过程感觉效果不错。其实,webservice的本质就是一个遵循soap协议的远程调用过程。无论是AXIS2、Xfire还是httpclient,其只是帮我们封装好了一些调用方法,方便我们的调用。我们完全可以不通过这些方法,通过java提供的标准api,封装一个soap请求,进行http请求,完成我们的调用过程(这里讨论的不包含webservice发布问题)。这也是为什么基本上每一个面向公众的webservice商业应用都会提供Endpoint、Disco、WSDL三种参数入口:

代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
/**
 * 类作用:演示了如何通过get、post以及httpclient封装soap请求三种方式调用webservice
 * @author qsw-Myonlystar @date 2010-7-6
 * @mail i@qinshuwei.com
 * 说明:调用的webservice为http://webservice.webxml.com.cn网站的火车站查询服务
 */
public class Test {
	/**
	 * 通过get方式演示webservice
	 */
	private void getMethodWS(){
		DefaultHttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet("http://webservice.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getStationName");
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		String responseBody = null;
		try {
			responseBody = httpclient.execute(httpget, responseHandler);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(responseBody);
		httpclient.getConnectionManager().shutdown();	
	}
	/**
	 * 通过post方式调用webservice
	 * @throws UnsupportedEncodingException
	 */
	private void postMethodWS(){
		DefaultHttpClient httpclient = new DefaultHttpClient();
		HttpPost httpost = new HttpPost(
				"http://webservice.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getStationAndTimeByTrainCode");
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("TrainCode",null));
		nvps.add(new BasicNameValuePair("UserID", null));
		UrlEncodedFormEntity urlEntity = null;
		try {
			urlEntity = new UrlEncodedFormEntity(nvps,
					HTTP.UTF_8);
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		httpost.setEntity(urlEntity);
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		String responseBody = null;
		try {
			responseBody = httpclient.execute(httpost, responseHandler);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(responseBody);
		httpclient.getConnectionManager().shutdown();	
	}
	/**
	 * 通过soap方式调用webservice
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	private void soapMethodWS(){
 
		String soapRequestData = "<?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>"
				+ "<getStationName xmlns=\"http://WebXml.com.cn/\" />"
				+ "</soap:Body>" + "</soap:Envelope>";
		DefaultHttpClient httpclient = new DefaultHttpClient();
		HttpPost httpost = new HttpPost("http://webservice.webxml.com.cn/WebServices/TrainTimeWebService.asmx"); 
		httpost.setHeader("Content-Type", "text/xml; charset=utf-8");
		HttpEntity entity = null;
		try {
			entity = new StringEntity(soapRequestData);
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
		httpost.setEntity(entity);
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		String responseBody = null;
		try {
			responseBody = httpclient.execute(httpost, responseHandler);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(responseBody);
		httpclient.getConnectionManager().shutdown();
	}
 
	/**
	 * @param args
	 * @throws IOException
	 * @throws ClientProtocolException
	 */
	public static void main(String[] args){
		Test t=new Test();
		t.getMethodWS();//get方式测试
		t.soapMethodWS();//soap方式测试
		t.postMethodWS();//post方式测试
	}
 
}
喜欢本文,那就收藏到: Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 POCO网摘 添加到饭否 QQ书签 Digbuzz我挖网
  • No Related Post

发表您的评论