ITeye博客

Java模拟HTTP的Get和Post请求

wanglei0119 · 2009-11-22 20:48 · java基础 · Markdown

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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。