java模拟http get和post 提交

使用httpclient工具包commons-httpclient-3.1.jar,依赖commons-logging-1.0.4.jar和commons-codec-1.3.jar。 
Java代码 复制代码  收藏代码
  1. import java.io.BufferedReader;   
  2. import java.io.IOException;   
  3. import java.io.InputStreamReader;   
  4. import java.util.Map;   
  5.   
  6. import org.apache.commons.httpclient.HttpClient;   
  7. import org.apache.commons.httpclient.HttpMethod;   
  8. import org.apache.commons.httpclient.HttpStatus;   
  9. import org.apache.commons.httpclient.URIException;   
  10. import org.apache.commons.httpclient.methods.GetMethod;   
  11. import org.apache.commons.httpclient.methods.PostMethod;   
  12. import org.apache.commons.httpclient.params.HttpMethodParams;   
  13. import org.apache.commons.httpclient.util.URIUtil;   
  14.   
  15. /**  
  16.  *   
  17.  *   
  18.  * <p>Title:HttpTookitEnhance</p>  
  19.  * <p>Description: httpclient模拟http请求,解决返回内容乱码问题</p>  
  20.  * <p>Copyright: Copyright (c) 2010</p>  
  21.  * <p>Company: </p>  
  22.  * @author libin  
  23.  * @version 1.0.0  
  24.  */  
  25. public class HttpTookitEnhance   
  26. {   
  27.       /**   
  28.        * 执行一个HTTP GET请求,返回请求响应的HTML   
  29.        *   
  30.        * @param url                 请求的URL地址   
  31.        * @param queryString 请求的查询参数,可以为null   
  32.        * @param charset         字符集   
  33.        * @param pretty            是否美化   
  34.        * @return 返回请求响应的HTML   
  35.        */  
  36.       public static String doGet ( String url, String queryString, String charset, boolean pretty )   
  37.       {   
  38.             StringBuffer response = new StringBuffer();   
  39.             HttpClient client = new HttpClient();   
  40.             HttpMethod method = new GetMethod(url);   
  41.             try  
  42.             {   
  43.                   if ( queryString != null && !queryString.equals("") )   
  44.                         //对get请求参数做了http请求默认编码,好像没有任何问题,汉字编码后,就成为%式样的字符串    
  45.                         method.setQueryString(URIUtil.encodeQuery(queryString));   
  46.                   client.executeMethod(method);   
  47.                   if ( method.getStatusCode() == HttpStatus.SC_OK )   
  48.                   {   
  49.                         BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), charset));   
  50.                         String line;   
  51.                         while ( ( line = reader.readLine() ) != null )   
  52.                         {   
  53.                               if ( pretty )   
  54.                                     response.append(line).append(System.getProperty("line.separator"));   
  55.                               else  
  56.                                     response.append(line);   
  57.                         }   
  58.                         reader.close();   
  59.                   }   
  60.             }   
  61.             catch ( URIException e )   
  62.             {   
  63.             }   
  64.             catch ( IOException e )   
  65.             {   
  66.             }   
  67.             finally  
  68.             {   
  69.                   method.releaseConnection();   
  70.             }   
  71.             return response.toString();   
  72.       }   
  73.   
  74.       /**   
  75.        * 执行一个HTTP POST请求,返回请求响应的HTML   
  76.        *   
  77.        * @param url         请求的URL地址   
  78.        * @param params    请求的查询参数,可以为null   
  79.        * @param charset 字符集   
  80.        * @param pretty    是否美化   
  81.        * @return 返回请求响应的HTML   
  82.        */  
  83.       public static String doPost ( String url, Map<String, String> params, String charset, boolean pretty )   
  84.       {   
  85.             StringBuffer response = new StringBuffer();   
  86.             HttpClient client = new HttpClient();   
  87.             HttpMethod method = new PostMethod(url);   
  88.             //设置Http Post数据    
  89.             if ( params != null )   
  90.             {   
  91.                   HttpMethodParams p = new HttpMethodParams();   
  92.                   for ( Map.Entry<String, String> entry : params.entrySet() )   
  93.                   {   
  94.                         p.setParameter(entry.getKey(), entry.getValue());   
  95.                   }   
  96.                   method.setParams(p);   
  97.             }   
  98.             try  
  99.             {   
  100.                   client.executeMethod(method);   
  101.                   if ( method.getStatusCode() == HttpStatus.SC_OK )   
  102.                   {   
  103.                         BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), charset));   
  104.                         String line;   
  105.                         while ( ( line = reader.readLine() ) != null )   
  106.                         {   
  107.                               if ( pretty )   
  108.                                     response.append(line).append(System.getProperty("line.separator"));   
  109.                               else  
  110.                                     response.append(line);   
  111.                         }   
  112.                         reader.close();   
  113.                   }   
  114.             }   
  115.             catch ( IOException e )   
  116.             {   
  117.             }   
  118.             finally  
  119.             {   
  120.                   method.releaseConnection();   
  121.             }   
  122.             return response.toString();   
  123.       }   
  124.   
  125.       public static void main ( String [] args )   
  126.       {   
  127.             String y = doGet("http://video.sina.com.cn/life/tips.html"null"GBK"true);   
  128.             System.out.println(y);   
  129.       }   
  130.   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值