1. package com.tw.url.util;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.Map;
  7.  
  8. import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
  9. import org.apache.commons.httpclient.Header;
  10. import org.apache.commons.httpclient.HttpClient;
  11. import org.apache.commons.httpclient.HttpException;
  12. import org.apache.commons.httpclient.HttpMethod;
  13. import org.apache.commons.httpclient.HttpStatus;
  14. import org.apache.commons.httpclient.methods.GetMethod;
  15. import org.apache.commons.httpclient.methods.PostMethod;
  16. import org.apache.commons.httpclient.params.HttpMethodParams;
  17.  
  18. /**
  19. * <p>
  20. * HTTP公用类
  21. * 所需包:Commons-httpclient.jar,commons-codec-1.3.jar
  22. * 学习参见网址: https://www.ibm.com/developerworks/cn/opensource/os-cn-crawler/
  23. * </p>
  24. *
  25. * @author tw 2009-07-16
  26. *
  27. */
  28. public class HttpClientUtils {
  29.  
  30.  
  31. public static void main(String arg[])throws Exception {
  32. String url = "https://www.99bill.com/webapp/receiveDrawbackAction.do";
  33. //getDoGetURL2(url,"utf-8");//测试ok
  34. //getDoGetURL(url,"utf-8");//测试ok
  35. getDoPostResponseDataByURL(url, null, "utf-8", true); //测试ok
  36. }
  37.  
  38. /**
  39. * <p>httpClient的get请求方式</p>
  40. * @param url = "https://www.99bill.com/webapp/receiveDrawbackAction.do";
  41. * @param charset = ="utf-8";
  42. * @return
  43. * @throws Exception
  44. */
  45. public static String getDoGetURL(String url, String charset)throws Exception {
  46.  
  47. HttpClient client = new HttpClient();
  48. GetMethod method1 = new GetMethod(url);
  49.  
  50. if (null == url || !url.startsWith("http")) {
  51. throw new Exception("请求地址格式不对");
  52. }
  53.  
  54. // 设置请求的编码方式
  55. if (null != charset) {
  56. method1.addRequestHeader("Content-Type",
  57. "application/x-www-form-urlencoded; charset=" + charset);
  58. } else {
  59. method1.addRequestHeader("Content-Type",
  60. "application/x-www-form-urlencoded; charset=" + "utf-8");
  61. }
  62. int statusCode = client.executeMethod(method1);
  63.  
  64. if (statusCode != HttpStatus.SC_OK) {// 打印服务器返回的状态
  65. System.out.println("Method failed: " + method1.getStatusLine());
  66. }
  67. // 返回响应消息
  68. byte[] responseBody = method1.getResponseBodyAsString().getBytes(method1.getResponseCharSet());
  69. // 在返回响应消息使用编码(utf-8或gb2312)
  70. String response = new String(responseBody, "utf-8");
  71. System.out.println("------------------response:"+response);
  72. // 释放连接
  73. method1.releaseConnection();
  74. return response;
  75. }
  76.  
  77.  
  78. /**
  79. * <p>httpClient的get请求方式2</p>
  80. * @param url = "https://www.99bill.com/webapp/receiveDrawbackAction.do";
  81. * @param charset = ="utf-8";
  82. * @return
  83. * @throws Exception
  84. */
  85. public static String getDoGetURL2(String url, String charset)
  86. throws Exception {
  87. /*
  88. * 使用 GetMethod 来访问一个 URL 对应的网页,实现步骤:
  89. * 1:生成一个 HttpClinet 对象并设置相应的参数。
  90. * 2:生成一个 GetMethod 对象并设置响应的参数。
  91. * 3:用 HttpClinet 生成的对象来执行 GetMethod 生成的Get 方法。
  92. * 4:处理响应状态码。
  93. * 5:若响应正常,处理 HTTP 响应内容。
  94. * 6:释放连接。
  95. */
  96.  
  97. /* 1 生成 HttpClinet 对象并设置参数 */
  98. HttpClient httpClient = new HttpClient();
  99. // 设置 Http 连接超时为5秒
  100. httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
  101.  
  102. /* 2 生成 GetMethod 对象并设置参数 */
  103. GetMethod getMethod = new GetMethod(url);
  104. // 设置 get 请求超时为 5 秒
  105. getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);
  106. // 设置请求重试处理,用的是默认的重试处理:请求三次
  107. getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler());
  108.  
  109. String response ="";
  110. /* 3 执行 HTTP GET 请求 */
  111. try {
  112. int statusCode = httpClient.executeMethod(getMethod);
  113. /* 4 判断访问的状态码 */
  114. if (statusCode != HttpStatus.SC_OK) {
  115. System.err.println("Method failed: "+ getMethod.getStatusLine());
  116. }
  117.  
  118. /* 5 处理 HTTP 响应内容 */
  119. // HTTP响应头部信息,这里简单打印
  120. Header[] headers = getMethod.getResponseHeaders();
  121. for (Header h : headers)
  122. System.out.println(h.getName() + "------------ " + h.getValue());
  123.  
  124. // 读取 HTTP 响应内容,这里简单打印网页内容
  125. byte[] responseBody = getMethod.getResponseBody();// 读取为字节数组
  126. response = new String(responseBody, charset);
  127. System.out.println("----------response:"+response);
  128.  
  129. // 读取为 InputStream,在网页内容数据量大时候推荐使用
  130. //InputStream response = getMethod.getResponseBodyAsStream();
  131.  
  132. } catch (HttpException e) {
  133. // 发生致命的异常,可能是协议不对或者返回的内容有问题
  134. System.out.println("Please check your provided http address!");
  135. e.printStackTrace();
  136. } catch (IOException e) {
  137. // 发生网络异常
  138. e.printStackTrace();
  139. } finally {
  140. /* 6 .释放连接 */
  141. getMethod.releaseConnection();
  142. }
  143. return response;
  144. }
  145.  
  146. /**
  147. * <p>执行一个HTTP POST请求,返回请求响应的HTML</p>
  148. *
  149. * @param url 请求的URL地址
  150. * @param params 请求的查询参数,可以为null
  151. * @param charset 字符集
  152. * @param pretty 是否美化
  153. * @return 返回请求响应的HTML
  154. */
  155. public static String getDoPostResponseDataByURL(String url,
  156. Map<String, String> params, String charset, boolean pretty) {
  157.  
  158. StringBuffer response = new StringBuffer();
  159.  
  160. HttpClient client = new HttpClient();
  161. HttpMethod method = new PostMethod(url);
  162.  
  163. //设置Http Post数据
  164. if (params != null) {
  165. HttpMethodParams p = new HttpMethodParams();
  166. for (Map.Entry<String, String> entry : params.entrySet()) {
  167. p.setParameter(entry.getKey(), entry.getValue());
  168. }
  169. method.setParams(p);
  170. }
  171. try {
  172. client.executeMethod(method);
  173. if (method.getStatusCode() == HttpStatus.SC_OK) {
  174. //读取为 InputStream,在网页内容数据量大时候推荐使用
  175. BufferedReader reader = new BufferedReader(
  176. new InputStreamReader(method.getResponseBodyAsStream(),
  177. charset));
  178. String line;
  179. while ((line = reader.readLine()) != null) {
  180. if (pretty)
  181. response.append(line).append(System.getProperty("line.separator"));
  182. else
  183. response.append(line);
  184. }
  185. reader.close();
  186. }
  187. } catch (IOException e) {
  188. System.out.println("执行HTTP Post请求" + url + "时,发生异常!");
  189. e.printStackTrace();
  190. } finally {
  191. method.releaseConnection();
  192. }
  193. System.out.println("--------------------"+response.toString());
  194. return response.toString();
  195. }
  196.  
  197.  
  198. }