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                     

 

posted on 2017-11-05 19:29  Sharpest  阅读(17982)  评论(0编辑  收藏  举报