我们爱分享---HttpClient4之模拟http请求

pl3121605999 2011-08-29 05:03:10
加精
我来给大家分享使用HttpClient4来模拟http请求,
本来想用csdn作为实验的,但是害怕版主删帖,所以我就来模拟百度贴吧的登录和回吧。
废话不多说看代码看注释吧。。。


package com.li.utli;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;
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.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;

import com.li.bean.Result;

/**
* 发送请求
* @author Legend、
*
*/

public class SendRequest {

//这是模拟get请求
public static Result sendGet(String url,Map<String,String> headers,Map<String,String> params,String encoding,boolean duan) throws ClientProtocolException, IOException{
//实例化一个Httpclient的
DefaultHttpClient client = new DefaultHttpClient();
//如果有参数的就拼装起来
url = url+(null==params?"":assemblyParameter(params));
//这是实例化一个get请求
HttpGet hp = new HttpGet(url);
//如果需要头部就组装起来
if(null!=headers)hp.setHeaders(assemblyHeader(headers));
//执行请求后返回一个HttpResponse
HttpResponse response = client.execute(hp);
//如果为true则断掉这个get请求
if(duan) hp.abort();
//返回一个HttpEntity
HttpEntity entity = response.getEntity();
//封装返回的参数
Result result= new Result();
//设置返回的cookie
result.setCookie(assemblyCookie(client.getCookieStore().getCookies()));
//设置返回的状态
result.setStatusCode(response.getStatusLine().getStatusCode());
//设置返回的头部信心
result.setHeaders(response.getAllHeaders());
//设置返回的信息
result.setHttpEntity(entity);
return result;
}
public static Result sendGet(String url,Map<String,String> headers,Map<String,String> params,String encoding) throws ClientProtocolException, IOException{
return sendGet(url, headers, params, encoding,false);
}

//这是模拟post请求
public static Result sendPost(String url,Map<String,String> headers,Map<String,String> params,String encoding) throws ClientProtocolException, IOException{
//实例化一个Httpclient的
DefaultHttpClient client = new DefaultHttpClient();
//实例化一个post请求
HttpPost post = new HttpPost(url);

//设置需要提交的参数
List<NameValuePair> list = new ArrayList<NameValuePair>();
for (String temp : params.keySet()) {
list.add(new BasicNameValuePair(temp,params.get(temp)));
}
post.setEntity(new UrlEncodedFormEntity(list,encoding));

//设置头部
if(null!=headers)post.setHeaders(assemblyHeader(headers));

//实行请求并返回
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();

//封装返回的参数
Result result = new Result();
//设置返回状态代码
result.setStatusCode(response.getStatusLine().getStatusCode());
//设置返回的头部信息
result.setHeaders(response.getAllHeaders());
//设置返回的cookie信心
result.setCookie(assemblyCookie(client.getCookieStore().getCookies()));
//设置返回到信息
result.setHttpEntity(entity);
return result ;
}

//这是组装头部
public static Header[] assemblyHeader(Map<String,String> headers){
Header[] allHeader= new BasicHeader[headers.size()];
int i = 0;
for (String str :headers.keySet()) {
allHeader[i] = new BasicHeader(str,headers.get(str));
i++;
}
return allHeader;
}

//这是组装cookie
public static String assemblyCookie(List<Cookie> cookies){
StringBuffer sbu = new StringBuffer();
for (Cookie cookie : cookies) {
sbu.append(cookie.getName()).append("=").append(cookie.getValue()).append(";");
}
if(sbu.length()>0)sbu.deleteCharAt(sbu.length()-1);
return sbu.toString();
}
//这是组装参数
public static String assemblyParameter(Map<String,String> parameters){
String para = "?";
for (String str :parameters.keySet()) {
para+=str+"="+parameters.get(str)+"&";
}
return para.substring(0,para.length()-1);
}
}


package com.li.bean;

import java.util.HashMap;

import org.apache.http.Header;
import org.apache.http.HttpEntity;

/**
* 封住请求返回的参数
* @author Legend、
*
*/

public class Result {

private String cookie;
private int statusCode;
private HashMap<String, Header> headerAll;
private HttpEntity httpEntity;

public String getCookie() {
return cookie;
}
public void setCookie(String cookie) {
this.cookie = cookie;
}
public int getStatusCode() {
return statusCode;
}
public void setStatusCode(int statusCode) {
this.statusCode = statusCode;
}
public HashMap<String, Header> getHeaders() {
return headerAll;
}

public void setHeaders(Header[] headers){
headerAll = new HashMap<String, Header>();
for (Header header : headers) {
headerAll.put(header.getName(), header);
}
}
public HttpEntity getHttpEntity() {
return httpEntity;
}
public void setHttpEntity(HttpEntity httpEntity) {
this.httpEntity = httpEntity;
}
}




package com.li.utli;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* 根据正则从HTML中提取响应的数据
* @author Legend、
*
*/

public class HtmlParse {

public static List<String> prase(String html,String regex,int number){

Pattern patten = Pattern.compile(regex);
Matcher mat = patten.matcher(html);
List<String> list = new ArrayList<String>();
while(mat.find()) {
if(number==-1){
list.add(mat.group());
continue;
}
if(number>0){
list.add(mat.group());
number--;
}else{
break;
}
}
return list;
}

public static List<String> prase(String html,String regex){
return prase(html, regex, -1);
}

}



package com.li.main;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JOptionPane;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.util.EntityUtils;

import com.li.bean.Result;
import com.li.utli.HtmlParse;
import com.li.utli.SendRequest;
import com.li.utli.VerificationcCode;

/**
* 百度贴吧的发帖及其回贴
* @author Legend、
*
*/
public class BaiduTieBaNDWS {

public static String reply(String content, String postsUrl,String cookie) throws ClientProtocolException, IOException {


//这是一些可有可无的头部信息,有的时候不需要,但是有的时候确需要,所以建议大家最好加上!
Map<String,String> headers = new HashMap<String,String>();
headers.put("Referer", postsUrl);
headers.put("Host", "tieba.baidu.com");
headers.put("Cookie",cookie);

//这是从帖子中获取一些发帖时候必备的参数
String html = EntityUtils.toString(SendRequest.sendGet(postsUrl, headers, null, "utf-8").getHttpEntity(),"utf-8");

String needParametersResolve[] = HtmlParse.prase(html, "kw:'.+',ie:'utf-8',rich_text:'\\d+',floor_num:'\\d+',fid:'\\d+',tid:'\\d+',tfrom:'\\d+',user_type:'\\d+'").get(0).replaceAll("'", "").split(",");

String floor_num = needParametersResolve[3].split(":")[1];
String fid = needParametersResolve[4].split(":")[1];

String tid = needParametersResolve[5].split(":")[1];
String kw =needParametersResolve[0].split(":")[1];


String vk_code = EntityUtils.toString(SendRequest.sendGet("http://tieba.baidu.com/f/user/json_vcode?lm="+fid+"&rs10=2&rs1=0&t=0.5954980056343667", null, null, "utf-8").getHttpEntity(),"utf-8");
String code = vk_code.split("\"")[7];
String tbs = EntityUtils.toString(SendRequest.sendGet("http://tieba.baidu.com/dc/common/tbs?t=0.17514605234768638", headers, null, "utf-8").getHttpEntity(),"utf-8").split("\"")[3];

//这是下载验证码
VerificationcCode.showGetVerificationcCode("http://tieba.baidu.com/cgi-bin/genimg?"+code, null,"e:/1.png");

//设置提交所有的参数
Map<String,String> parameters = new HashMap<String,String>();
parameters.put("add_post_submit", "发 表 ");
parameters.put("kw", kw);
parameters.put("floor_num", floor_num);
parameters.put("ie", "utf-8");
parameters.put("rich_text", "1");
parameters.put("hasuploadpic", "0");
parameters.put("fid",fid);
parameters.put("rich_text", "1");
parameters.put("tid", tid);
parameters.put("hasuploadpic", "0");
parameters.put("picsign", "");
parameters.put("quote_id", "0");
parameters.put("useSignName", "on");
parameters.put("content", content);
parameters.put("vcode_md5", code);
parameters.put("tbs", tbs);
parameters.put("vcode", JOptionPane.showInputDialog(null,
"<html><img src=\"file:///e:/1.png\" width=\33\" height=\55\"><br><center>请输入验证码</center><br></html>"));

//准备好一切,回帖
Result res = SendRequest.sendPost("http://tieba.baidu.com/f/commit/post/add", headers, parameters, "utf-8");

//回帖之后百度会返回一段json,说明是否回帖成功
return EntityUtils.toString(res.getHttpEntity(),"utf-8");
}

//百度登陆
public static String testAccount(String name, String password) throws ClientProtocolException, IOException {
Map<String,String> parameters = new HashMap<String,String>();
parameters.put("password", password);
parameters.put("username", name);
String str = SendRequest.sendPost("https://passport.baidu.com/?login", null, parameters,"utf-8").getCookie();
return str;
}

}

...全文
15295 156 打赏 收藏 转发到动态 举报
写回复
用AI写文章
156 条回复
切换为时间正序
请发表友善的回复…
发表回复
yaerfeng 2014-05-05
  • 打赏
  • 举报
回复
可以参考代码:java apache commons HttpClient发送get和post请求的学习整理,下载地址:http://www.zuidaima.com/share/1754065983409152.htm
Mark0904 2014-03-18
  • 打赏
  • 举报
回复
支持楼主的分享精神,写得很详细
liguanlihuan 2014-03-07
  • 打赏
  • 举报
回复
好东西,值得学习.
xkswz 2013-12-06
  • 打赏
  • 举报
回复
后悔现在才看到…研究了
hukobe 2013-08-01
  • 打赏
  • 举报
回复
能模拟IP吗,貌似源码中没看到模拟IP的。
zhaolinzzu 2013-07-31
  • 打赏
  • 举报
回复
{"no":265,"error":"","data":{"autoMsg":"","fid":2829320,"fname":"\u935a\u70b2\u6ad6\u93c4\u71ba\u2516","tid":1210817094,"is_login":0,"content":"\u6769\u6b0e\u91dc\u7490\u5bf8\u5ada\u9417\u6c62\u935f\u5a4f\u7d12\u951b","vcode":{"need_vcode":0,"str_reason":"","captcha_vcode_str":"","captcha_code_type":0,"userstatevcode":0}}} 这个登录的问题吗
zhaolinzzu 2013-07-31
  • 打赏
  • 举报
回复
楼主,我这里返回这个is_login 为0 ,登录失败吗? {"no":265,"error":"","data":{"autoMsg":"","fid":2829320,"fname":"\u935a\u70b2\u6ad6\u93c4\u71ba\u2516","tid":1210817094,"is_login":0,"content":"\u6769\u6b0e\u91dc\u7490\u5bf8\u5ada\u9417\u6c62\u935f\u5a4f\u7d12\u951b","vcode":{"need_vcode":0,"str_reason":"","captcha_vcode_str":"","captcha_code_type":0,"userstatevcode":0}}}
IT_Elite0 2013-07-29
  • 打赏
  • 举报
回复
留名。。。学习了。。。
NotHys 2013-07-26
  • 打赏
  • 举报
回复
楼主登录百度的是以前的老版本的现在百度的登录有v2了,楼主试试怎么登录百度啊!用httpclient4.x我没有做出来了,httpclient3.X的我写出来了可以使用!楼主也试试。交流一下
haohaoxuexi250 2013-07-19
  • 打赏
  • 举报
回复
你好 你写的相当精彩 但是还是有点不懂 求指点 能发我一个论坛自动回帖的例子 用于学习研究 2824924310@qq.com
tianlihu12531 2013-04-11
  • 打赏
  • 举报
回复
留名 学习 HttpCliemt4
a455642158 2013-04-03
  • 打赏
  • 举报
回复
httpclient的get访问方式,禁止自动跳转后,模拟重定向谁有例子啊,带cookie的……
乘桴游海 2013-03-07
  • 打赏
  • 举报
回复
mark,好好研究研究。楼主好人。。。
meteor1015 2012-10-31
  • 打赏
  • 举报
回复
mark !
fengbin041990 2012-09-27
  • 打赏
  • 举报
回复
现在贴吧抓包工具抓到的是kw=%E5%9C%B0%E4%B8%8B%E5%9F%8E%E4%B8%8E%E5%8B%87%E5%A3%AB&ie=utf-8&rich_text=1&floor_num=5&fid=81570&tid=1858480297&mouse_pwd=34%2C36%2C37%2C60%2C33%2C40%2C38%2C38%2C25%2C33%2C60%2C32%2C60%2C33%2C60%2C32%2C60%2C33%2C60%2C32%2C60%2C33%2C60%2C32%2C60%2C33%2C60%2C32%2C25%2C33%2C39%2C40%2C37%2C34%2C25%2C33%2C34%2C40%2C32%2C60%2C40%2C32%2C32%2C13487547681161&mouse_pwd_t=1348754768116&mouse_pwd_isclick=1&content=.&anonymous=0&tbs=6f35e6ebb716cdd61348754766&vcode_md5=
我用正则表达式怎么不行?String needParametersResolve[] = HtmlParse.prase(html, "kw:'.+',ie:'utf-8',rich_text:'\\d+',floor_num:'\\d+',fid:'\\d+',tid:'\\d+'").get(0).replaceAll("'", "").split(",");该怎么该啊,冒号改=吗?
未来需要奋斗 2012-09-27
  • 打赏
  • 举报
回复
怎么设代理啊 我执行到验证码那块就不走了
liang523894408 2012-07-23
  • 打赏
  • 举报
回复
UP 楼主威武
lovesisizx5 2012-07-09
  • 打赏
  • 举报
回复
这个帖子是在太NB了,啥也不说了
ourmeng19861001 2012-06-27
  • 打赏
  • 举报
回复
测试成功 欣喜
zlb0hao 2012-06-11
  • 打赏
  • 举报
回复
能说说HttpClient4有哪些值得赞赏的地方吗?
加载更多回复(130)

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧