httputil用http獲取請求的工具類


  1                         package com.xiaocan.demo.util;
  2 
  3 import java.io.IOException;
  4 import java.io.InputStream;
  5 import java.util.HashMap;
  6 import java.util.Iterator;
  7 import java.util.Map;
  8 import java.util.Map.Entry;
  9 
 10 import org.apache.commons.httpclient.HttpClient;
 11 import org.apache.commons.httpclient.HttpStatus;
 12 import org.apache.commons.httpclient.methods.GetMethod;
 13 import org.apache.commons.httpclient.methods.PostMethod;
 14 import org.apache.commons.httpclient.params.HttpMethodParams;
 15 import org.apache.commons.io.IOUtils;
 16 import org.apache.commons.lang.StringUtils;
 17 
 18 /**
 19  * <p>Http工具類
 20  * 
 21  * <p>Http工具類,為系統提供通用Http訪問操作方法:
 22  * 
 23  * <p>1、發送GET請求;
 24  * <p>2、發送POST請求。
 25  * 
 26  */
 27 public class HttpUtil {
 28 
 29     /**
 30      * <p>發送GET請求
 31      * 
 32      * @param  url GET請求地址
 33      * 
 34      * @return 與當前請求對應的響應內容字節數組
 35      * 
 36      */
 37     public static byte[] doGet(String url) {
 38 
 39         return HttpUtil.doGet(url , null , null , 0);
 40     }
 41 
 42     /**
 43      * <p>發送GET請求
 44      * 
 45      * @param  url       GET請求地址
 46      * @param  headerMap GET請求頭參數容器
 47      * 
 48      * @return 與當前請求對應的響應內容字節數組
 49      * 
 50      */
 51     public static byte[] doGet(String url , Map headerMap) {
 52 
 53         return HttpUtil.doGet(url , headerMap , null , 0);
 54     }
 55 
 56     /**
 57      * <p>發送GET請求
 58      * 
 59      * @param  url       GET請求地址
 60      * @param  proxyUrl  代理服務器地址
 61      * @param  proxyPort 代理服務器端口號
 62      * 
 63      * @return 與當前請求對應的響應內容字節數組
 64      * 
 65      * @modify 竇海寧, 2012-03-19
 66      */
 67     public static byte[] doGet(String url , String proxyUrl , int proxyPort) {
 68 
 69         return HttpUtil.doGet(url , null , proxyUrl , proxyPort);
 70     }
 71 
 72     /**
 73      * <p>發送GET請求
 74      * 
 75      * @param  url       GET請求地址
 76      * @param  headerMap GET請求頭參數容器
 77      * @param  proxyUrl  代理服務器地址
 78      * @param  proxyPort 代理服務器端口號
 79      * 
 80      * @return 與當前請求對應的響應內容字節數組
 81      * 
 82      * @modify 竇海寧, 2012-03-19
 83      */
 84     public static byte[] doGet(String url , Map headerMap , String proxyUrl , int proxyPort) {
 85 
 86         byte[]     content    = null;
 87         HttpClient httpClient = new HttpClient();
 88         GetMethod  getMethod  = new GetMethod(url);
 89 
 90         if (headerMap != null) {
 91 
 92             //頭部請求信息
 93             if (headerMap != null) {
 94 
 95                 Iterator iterator = headerMap.entrySet().iterator();
 96                 while (iterator.hasNext()) {
 97 
 98                     Entry entry = (Entry) iterator.next();
 99                     getMethod.addRequestHeader(entry.getKey().toString() , entry.getValue().toString());
100                 }
101             }
102         }
103 
104         if (StringUtils.isNotBlank(proxyUrl)) {
105 
106             httpClient.getHostConfiguration().setProxy(proxyUrl , proxyPort);
107         }
108 
109         //設置成了默認的恢復策略,在發生異常時候將自動重試3次,在這里你也可以設置成自定義的恢復策略
110         getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT , 10000);
111         //postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER , new DefaultHttpMethodRetryHandler());
112         InputStream inputStream = null;
113         try {
114 
115             if (httpClient.executeMethod(getMethod) == HttpStatus.SC_OK) {
116 
117                 //讀取內容
118                 inputStream = getMethod.getResponseBodyAsStream();
119                 content     = IOUtils.toByteArray(inputStream);
120             } else {
121 
122                 System.err.println("Method failed: " + getMethod.getStatusLine());
123             }
124         } catch (IOException ex) {
125 
126             ex.printStackTrace();
127         } finally {
128 
129             IOUtils.closeQuietly(inputStream);
130             getMethod.releaseConnection();
131         }
132         return content;
133     }
134 
135     /**
136      * <p>發送POST請求
137      * 
138      * @param  url          POST請求地址
139      * @param  parameterMap POST請求參數容器
140      * 
141      * @return 與當前請求對應的響應內容字節數組
142      * 
143      */
144     public static byte[] doPost(String url , Map parameterMap) {
145 
146         return HttpUtil.doPost(url , null , parameterMap , null , null , 0);
147     }
148 
149     /**
150      * <p>發送POST請求
151      * 
152      * @param  url          POST請求地址
153      * @param  parameterMap POST請求參數容器
154      * @param  paramCharset 參數字符集名稱
155      * 
156      * @return 與當前請求對應的響應內容字節數組
157      * 
158      * @modify 竇海寧, 2012-05-21
159      */
160     public static byte[] doPost(String url , Map parameterMap , String paramCharset) {
161 
162         return HttpUtil.doPost(url , null , parameterMap , paramCharset , null , 0);
163     }
164 
165     /**
166      * <p>發送POST請求
167      * 
168      * @param  url          POST請求地址
169      * @param  headerMap    POST請求頭參數容器
170      * @param  parameterMap POST請求參數容器
171      * @param  paramCharset 參數字符集名稱
172      * 
173      * @return 與當前請求對應的響應內容字節數組
174      * 
175      * @modify 竇海寧, 2012-05-21
176      */
177     public static byte[] doPost(String url , Map headerMap , Map parameterMap , String paramCharset) {
178 
179         return HttpUtil.doPost(url , headerMap , parameterMap , paramCharset , null , 0);
180     }
181 
182     /**
183      * <p>發送POST請求
184      * 
185      * @param  url          POST請求地址
186      * @param  parameterMap POST請求參數容器
187      * @param  paramCharset 參數字符集名稱
188      * @param  proxyUrl     代理服務器地址
189      * @param  proxyPort    代理服務器端口號
190      * 
191      * @return 與當前請求對應的響應內容字節數組
192      * 
193      */
194     public static byte[] doPost(String url , Map parameterMap , String paramCharset , String proxyUrl , int proxyPort) {
195 
196         return HttpUtil.doPost(url , null , parameterMap , paramCharset , proxyUrl , proxyPort);
197     }
198 
199     /**
200      * <p>發送POST請求
201      * 
202      * @param  url          POST請求地址
203      * @param  headerMap    POST請求頭參數容器
204      * @param  parameterMap POST請求參數容器
205      * @param  paramCharset 參數字符集名稱
206      * @param  proxyUrl     代理服務器地址
207      * @param  proxyPort    代理服務器端口號
208      * 
209      * @return 與當前請求對應的響應內容字節數組
210      * 
211      * @modify 竇海寧, 2012-05-21
212      */
213     public static byte[] doPost(String url , Map headerMap , Map parameterMap , String paramCharset , String proxyUrl , int proxyPort) {
214 
215         byte[]     content    = null;
216         HttpClient httpClient = new HttpClient();
217         PostMethod postMethod = new PostMethod(url);
218 
219         if (StringUtils.isNotBlank(paramCharset)) {
220 
221             postMethod.getParams().setContentCharset(paramCharset);
222             postMethod.getParams().setHttpElementCharset(paramCharset);
223         }
224 
225         if (headerMap != null) {
226 
227             //頭部請求信息
228             if (headerMap != null) {
229 
230                 Iterator iterator = headerMap.entrySet().iterator();
231                 while (iterator.hasNext()) {
232 
233                     Entry entry = (Entry) iterator.next();
234                     postMethod.addRequestHeader(entry.getKey().toString() , entry.getValue().toString());
235                 }
236             }
237         }
238 
239         Iterator iterator = parameterMap.keySet().iterator();
240         while (iterator.hasNext()) {
241 
242             String key = (String) iterator.next();
243             postMethod.addParameter(key , (String) parameterMap.get(key));
244         }
245 
246         if (StringUtils.isNotBlank(proxyUrl)) {
247 
248             httpClient.getHostConfiguration().setProxy(proxyUrl , proxyPort);
249         }
250 
251         //設置成了默認的恢復策略,在發生異常時候將自動重試3次,在這里你也可以設置成自定義的恢復策略
252         postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT , 10000);
253         //postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER , new DefaultHttpMethodRetryHandler());
254         InputStream inputStream = null;
255         try {
256 
257             if (httpClient.executeMethod(postMethod) == HttpStatus.SC_OK) {
258 
259                 //讀取內容
260                 inputStream = postMethod.getResponseBodyAsStream();
261                 content     = IOUtils.toByteArray(inputStream);
262             } else {
263 
264                 System.err.println("Method failed: " + postMethod.getStatusLine());
265             }
266         } catch (IOException ex) {
267 
268             ex.printStackTrace();
269         } finally {
270 
271             IOUtils.closeQuietly(inputStream);
272             postMethod.releaseConnection();
273         }
274         return content;
275     }
276     
277     public static void main(String[] args) {
278         Map<String, String> map = new HashMap<String, String>();
279         map.put("wd", "nima");
280         byte[] b = doGet("http://www.baidu.com", map);
281         System.out.println("-------------------"+new String(b));
282     }
283 
284 }
285 
286                     

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM