Java之網絡請求工具類(依賴:org.apache.http;注:HttpClient 4.4,HttpCore 4.4)

到此處能夠去下載依賴包:http://hc.apache.org/downloads.cgi java

 1 import java.util.List;
 2 
 3 import org.apache.http.HttpStatus;
 4 import org.apache.http.NameValuePair;
 5 import org.apache.http.client.config.RequestConfig;
 6 import org.apache.http.client.entity.UrlEncodedFormEntity;
 7 import org.apache.http.client.methods.CloseableHttpResponse;
 8 import org.apache.http.client.methods.HttpGet;
 9 import org.apache.http.client.methods.HttpPost;
10 import org.apache.http.impl.client.CloseableHttpClient;
11 import org.apache.http.impl.client.HttpClients;
12 import org.apache.http.util.EntityUtils;
13 
14 /**
15  * HttpServletUtil
16  *
17  * @author ysj
18  * @Date: 2015-1-30 下午2:07:55
19  */
20 public class HttpServletUtil {
21     private static CloseableHttpClient httpclient;
22     private static RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build();
23 
24     /**
25      * Post:訪問數據庫並返回數據字符串
26      * 
27      * @param params
28      *            向服務器端傳的參數
29      * @param url
30      * @return String 數據字符串
31      * @throws Exception
32      */
33     public static String doPost(List<NameValuePair> params, String url) throws Exception {
34         String result = null;
35         httpclient = HttpClients.createDefault();
36         HttpPost httpPost = new HttpPost(url);
37         httpPost.setEntity(new UrlEncodedFormEntity(params));
38         //設置請求和傳輸超時時間
39         httpPost.setConfig(requestConfig);
40         CloseableHttpResponse httpResp = httpclient.execute(httpPost);
41         try {
42             int statusCode = httpResp.getStatusLine().getStatusCode();
43             // 判斷是夠請求成功
44             if (statusCode == HttpStatus.SC_OK) {
45                 System.out.println("狀態碼:" + statusCode);
46                 System.out.println("請求成功!");
47                 // 獲取返回的數據
48                 result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
49             } else {
50                 System.out.println("狀態碼:"
51                         + httpResp.getStatusLine().getStatusCode());
52                 System.out.println("HttpPost方式請求失敗!");
53             }
54         } finally {
55             httpResp.close();
56             httpclient.close();
57         }
58         return result;
59     }
60 
61     /**
62      * Get:訪問數據庫並返回數據字符串
63      * 
64      * @param url
65      * @return String 數據字符串
66      * @throws Exception
67      */
68      public static String doGet(String url) throws Exception{
69         String result = null;
70         httpclient = HttpClients.createDefault();
71         HttpGet httpGet = new HttpGet(url);
72         //設置請求和傳輸超時時間
73         httpGet.setConfig(requestConfig);
74         CloseableHttpResponse httpResp = httpclient.execute(httpGet);
75         try {
76             int statusCode = httpResp.getStatusLine().getStatusCode();
77             // 判斷是夠請求成功
78             if (statusCode == HttpStatus.SC_OK) {
79                 System.out.println("狀態碼:" + statusCode);
80                 System.out.println("請求成功!");
81                 // 獲取返回的數據
82                 result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
83             } else {
84                 System.out.println("狀態碼:"
85                         + httpResp.getStatusLine().getStatusCode());
86                 System.out.println("HttpGet方式請求失敗!");
87             }
88         } finally {
89             httpResp.close();
90             httpclient.close();
91         }
92         return result;
93      }
94 }
相關文章
相關標籤/搜索