日期:2014-05-17  浏览次数:20611 次

httpclient4.0的简单使用(1)马上去偷网页吧~
最近需要用到httpclient做一些模拟登陆什么的..本文不涉及到验证码下载的处理..(因为我没用到…)

首先是下载地址

http://disk.idealweb.cn/disk/f/213_20120830185357_2173_httpcomponents-client-4.2.1-bin.tar.gz
ps:推荐一下上面自己做的网盘,不需要任何注册直接上传,可以外链..哈哈~disk.idealweb.cn

引入改引入的包.

主要的两种获取页面的方式.get post直接贴代码.
关键的都有注释..
真的很简单了,再配合上httpwatch和正则理论上大多数页面的数据都可以轻松抓取了
刷个链接..博客的地址...
http://www.wuliaoji.com/httpclient4-0%E7%9A%84%E7%AE%80%E5%8D%95%E4%BD%BF%E7%94%A8.html

Java code


package com.bms.core;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
 
import com.bms.util.CommonUtil;
 
public class Test2 {
 
    /**
     * @param args
     * @throws IOException 
     * @throws ClientProtocolException 
     */
    public static void main(String[] args) throws ClientProtocolException, IOException {
        DefaultHttpClient httpclient = new DefaultHttpClient();
 
         HttpGet httpGet = new HttpGet("http://www.baidu.com");
         String body = "";
         HttpResponse response;
         HttpEntity entity;
         response = httpclient.execute(httpGet);
         entity = response.getEntity();
         body = EntityUtils.toString(entity);//这个就是页面源码了
         httpGet.abort();//中断请求,接下来可以开始另一段请求
         System.out.println(body);
         //httpGet.releaseConnection();//释放请求.如果释放了相当于要清空session
         //以下是post方法
         HttpPost httpPost = new HttpPost("http://www.baidu.com");//一定要改成可以提交的地址,这里用百度代替
         List <NameValuePair> nvps = new ArrayList <NameValuePair>();
         nvps.add(new BasicNameValuePair("name", "1"));//名值对
         nvps.add(new BasicNameValuePair("account", "xxxx"));
         httpPost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
         response = httpclient.execute(httpPost);
         entity = response.getEntity();
         body = EntityUtils.toString(entity);
         System.out.println("Login form get: " + response.getStatusLine());//这个可以打印状态
         httpPost.abort();
         System.out.println(body);
         httpPost.releaseConnection();
    }
 
}




可能会有..httpclient4.0的简单使用(2)...

------解决方案--------------------
学习,感谢楼主分享