---
title: "Java模拟HTTP的Get和Post请求"
author: "wanglei0119"
date: "2009-11-22T20:48:38Z"
tags: []
slug: "java-http-get-post"
draft: false
lastmod: "2009-11-22T20:54:20Z"
id: 523218
url: "https://wanglei0119.iteye.com/blog/523218"
---

1. import org.apache.commons.httpclient.HttpClient;  
   2. import org.apache.commons.httpclient.HttpMethod;  
   3. import org.apache.commons.httpclient.HttpStatus;  
   4. import org.apache.commons.httpclient.URIException;  
   5. import org.apache.commons.httpclient.methods.GetMethod;  
   6. import org.apache.commons.httpclient.methods.PostMethod;  
   7. import org.apache.commons.httpclient.params.HttpMethodParams;  
   8. import org.apache.commons.httpclient.util.URIUtil;  
   9. import org.apache.commons.lang.StringUtils;  
  10. import org.apache.commons.logging.Log;  
  11. import org.apache.commons.logging.LogFactory;  
  12.   
  13. import java.io.BufferedReader;  
  14. import java.io.IOException;  
  15. import java.io.InputStreamReader;  
  16. import java.util.Map;  
  17.   
  18. /** 
  19. * HTTP工具箱 
  20. * 
  21. * @author leizhimin 2009-6-19 16:36:18 
  22. */  
  23. public final class HttpTookit {  
  24.         private static Log log = LogFactory.getLog(HttpTookit.class);  
  25.   
  26.         /** 
  27.          * 执行一个HTTP GET请求，返回请求响应的HTML 
  28.          * 
  29.          * @param url                 请求的URL地址 
  30.          * @param queryString 请求的查询参数,可以为null 
  31.          * @param charset         字符集 
  32.          * @param pretty            是否美化 
  33.          * @return 返回请求响应的HTML 
  34.          */  
  35.         public static String doGet(String url, String queryString, String charset, boolean pretty) {  
  36.                 StringBuffer response = new StringBuffer();  
  37.                 HttpClient client = new HttpClient();  
  38.                 HttpMethod method = new GetMethod(url);  
  39.                 try {  
  40.                         if (StringUtils.isNotBlank(queryString))  
  41.                                 //对get请求参数做了http请求默认编码，好像没有任何问题，汉字编码后，就成为%式样的字符串  
  42.                                 method.setQueryString(URIUtil.encodeQuery(queryString));  
  43.                         client.executeMethod(method);  
  44.                         if (method.getStatusCode() == HttpStatus.SC_OK) {  
  45.                                 BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), charset));  
  46.                                 String line;  
  47.                                 while ((line = reader.readLine()) != null) {  
  48.                                         if (pretty)  
  49.                                                 response.append(line).append(System.getProperty("line.separator"));  
  50.                                         else  
  51.                                                 response.append(line);  
  52.                                 }  
  53.                                 reader.close();  
  54.                         }  
  55.                 } catch (URIException e) {  
  56.                         log.error("执行HTTP Get请求时，编码查询字符串“" + queryString + "”发生异常！", e);  
  57.                 } catch (IOException e) {  
  58.                         log.error("执行HTTP Get请求" + url + "时，发生异常！", e);  
  59.                 } finally {  
  60.                         method.releaseConnection();  
  61.                 }  
  62.                 return response.toString();  
  63.         }  
  64.   
  65.         /** 
  66.          * 执行一个HTTP POST请求，返回请求响应的HTML 
  67.          * 
  68.          * @param url         请求的URL地址 
  69.          * @param params    请求的查询参数,可以为null 
  70.          * @param charset 字符集 
  71.          * @param pretty    是否美化 
  72.          * @return 返回请求响应的HTML 
  73.          */  
  74.         public static String doPost(String url, Map<String, String> params, String charset, boolean pretty) {  
  75.                 StringBuffer response = new StringBuffer();  
  76.                 HttpClient client = new HttpClient();  
  77.                 HttpMethod method = new PostMethod(url);  
  78.                 //设置Http Post数据  
  79.                 if (params != null) {  
  80.                         HttpMethodParams p = new HttpMethodParams();  
  81.                         for (Map.Entry<String, String> entry : params.entrySet()) {  
  82.                                 p.setParameter(entry.getKey(), entry.getValue());  
  83.                         }  
  84.                         method.setParams(p);  
  85.                 }  
  86.                 try {  
  87.                         client.executeMethod(method);  
  88.                         if (method.getStatusCode() == HttpStatus.SC_OK) {  
  89.                                 BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), charset));  
  90.                                 String line;  
  91.                                 while ((line = reader.readLine()) != null) {  
  92.                                         if (pretty)  
  93.                                                 response.append(line).append(System.getProperty("line.separator"));  
  94.                                         else  
  95.                                                 response.append(line);  
  96.                                 }  
  97.                                 reader.close();  
  98.                         }  
  99.                 } catch (IOException e) {  
 100.                         log.error("执行HTTP Post请求" + url + "时，发生异常！", e);  
 101.                 } finally {  
 102.                         method.releaseConnection();  
 103.                 }  
 104.                 return response.toString();  
 105.         }  
 106.   
 107.         public static void main(String args) {  
 108.                 String y = doGet("http://video.sina.com.cn/life/tips.html", null, "GBK", true);  
 109.                 System.out.println(y);  
 110.         }  
 111. }