httpclient 学习的几点经验

httpclinet 是一个java语言开源包 ,支持通过http协议下载各种文件,具有良好的配置性。详细见:http://hc.apache.org/httpclient-3.x/ 。

下面具体例举使用的经验(httpclient3.1):

1. 多线程

    private static HttpClient hc = null;

    hc = new HttpClient(new MultiThreadedHttpConnectionManager());

 2. 使用代理

    private static HttpClient hcproxy = null;

    hcproxy = new HttpClient(new MultiThreadedHttpConnectionManager());

    hcproxy.getHostConfiguration().setProxy("192.168.1.1", 8888);//ip and port

 3.含有中文url的处理

   GetMethod get = null;

   URI strURI = null;

   url="http://60.190.222.233:5583/bst28/0911/满文军--爱唱给你听.wma";
   try {
                strURI = new URI(url, true, "GBK");
         } catch (URIException e) {
                strURI = new URI(url, false, "GBK");
         }

   get = new GetMethod();
   get.setURI(strURI);

 4.抓取常见的httpclient基本设置

   get.setFollowRedirects(true);//设置重定向
   get.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                    new DefaultHttpMethodRetryHandler(5, false));//设置重试
   get.getParams().setParameter(HttpMethodParams.SO_TIMEOUT,
                    new Integer(5000));//设置超时
   get.getParams().setParameter(HttpMethodParams.USER_AGENT,"Nokia");//设置UA
   get.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);//设置Cookie
   get.setRequestHeader("Cookie","laystate=undefined;show160site=show160");

   get.addRequestHeader("referer", "");//设置referer

   UA,Cookie,Referer 是抓取页面时特别需要注意的几项设置。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值