1. 項目環(huán)境如下:
myeclipse6.5 、tomcat5.0、system:xp、JDK:開(kāi)發(fā)1.5,編譯1.4
為了方便,在原來(lái)的web項目UpDown中新建了一個(gè)httpcall包,用來(lái)保存http接口和調用的客戶(hù)端。
2.準備需要的jar包
* commons-httpclient-3.0.jar
* commons-logging.jar
* commons-codec-1.3.jar
3.class&method
HttpClient:
GetMethod:
PostMethod:
start
接口寫(xiě)了一個(gè)servlet來(lái)接收客戶(hù)端get/post的請求
web.xml需要加入以下配置:
TestHTTPServer
httpcall.TestHTTPServer
TestHTTPServer
/httpServer
TestHTTPServer.java的代碼如下:
TestHTTPServer
1 package httpcall;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import javax.servlet.ServletException;
6 import javax.servlet.http.HttpServlet;
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9
10
17
18 public class TestHTTPServer extends HttpServlet{
19
20 private static final long serialVersionUID = 1L;
21
22 public void doGet(HttpServletRequest request, HttpServletResponse response)
23 throws ServletException, IOException {
24 response.setCharacterEncoding("gbk");
25
26 PrintWriter out = response.getWriter();
27 String param1 = request.getParameter("param1");
28 out.println("param1=" + param1);
29 String param2 = request.getParameter("param2");
30 out.println("param2=" + param2);
31 if (param1 == null || "".equals("param1") || param1.length() <= 0) {
32 out.println("http call failed,參數param1不能為空,程序退出");
33 } else if (param2 == null || "".equals("param2")
34 || param2.length() <= 0) {
35 out.println("http call failed,參數param2不能為空,程序退出");
36 } else {
37 out.println("---http call success---");
38 }
39 out.close();
40 }
41
42 public void doPost(HttpServletRequest request, HttpServletResponse response)
43 throws ServletException, IOException {
44 this.doGet(request, response);
45 }
46 }
HttpClientUtil.java的代碼如下:
HttpClientUtil
1 package httpcall;
2
3 import java.io.IOException;
4 import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
5 //import org.apache.commons.httpclient.Header;
6 import org.apache.commons.httpclient.HttpClient;
7 import org.apache.commons.httpclient.HttpException;
8 //import org.apache.commons.httpclient.HttpStatus;
9 import org.apache.commons.httpclient.methods.GetMethod;
10 import org.apache.commons.httpclient.methods.PostMethod;
11 import org.apache.commons.httpclient.params.HttpMethodParams;
12 //import org.apache.commons.httpclient.NameValuePair;
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15
16
24
25 public class HttpClientUtil {
26
27 private static final Log log = LogFactory
28 .getLog(HttpClientUtil.class);
29
30
36 public static String getHttp(String param1,String param2){
37 String responseMsg = "";
38
39 // 1.構造HttpClient的實(shí)例
40 HttpClient httpClient = new HttpClient();
41
42 // 用于測試的http接口的url
43 String url="http://localhost:8080/UpDown/httpServer?param1="+param1+"¶m2="+param2;
44
45 // 2.創(chuàng )建GetMethod的實(shí)例
46 GetMethod getMethod = new GetMethod(url);
47
48 // 使用系統系統的默認的恢復策略
49 getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
50 new DefaultHttpMethodRetryHandler());
51
52 try {
53 //3.執行g(shù)etMethod,調用http接口
54 httpClient.executeMethod(getMethod);
55
56 //4.讀取內容
57 byte[] responseBody = getMethod.getResponseBody();
58
59 //5.處理返回的內容
60