HttpClient模拟登录回贴

本文介绍使用HttpClient4进行模拟登录及回帖的过程,包括设置登录参数、处理重定向及获取formhash等关键步骤。
经过HttpClient3 的N次处理,发现HttpClient3对Cookie的管理不是太好,总是传值出现问题,可以用HttpClient4一比较,发现他对Cookie的管理实在是太好了,不需要你去处理,下面是关于HttpClient4模拟登录回贴的代码,希望对用到这方面的朋友提供参考

package com.httpclient4; 
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
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.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
public class MainLogin {
 public static void main(String[] args) throws ClientProtocolException,
   IOException {
  HttpClient httpClient = new DefaultHttpClient();
  List<NameValuePair> formparams = new ArrayList<NameValuePair>();
  formparams.add(new BasicNameValuePair("u_name", "jbeduhai"));
  formparams.add(new BasicNameValuePair("u_passwd", "xxx"));
  UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams,
    "UTF-8");
  HttpPost httppost = new HttpPost("http://passport.xxx.com.cn/login.jsp");
  httppost.setEntity(entity);
  HttpContext localContext = new BasicHttpContext();
  HttpResponse response2 = httpClient.execute(httppost, localContext);
  int statusCode = response2.getStatusLine().getStatusCode();
  System.out.println("statusCode:" + statusCode);
  System.out.println(EntityUtils.toString(response2.getEntity(),"UTF-8"));
  EntityUtils.consume(response2.getEntity());
  
  
  if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
    || statusCode == HttpStatus.SC_MOVED_TEMPORARILY
    || statusCode == HttpStatus.SC_SEE_OTHER
    || statusCode == HttpStatus.SC_TEMPORARY_REDIRECT) {
   String newUrl = response2.getLastHeader("Location").getValue();
   System.out.println("从新定向的页面为:" + newUrl);
   HttpGet get = new HttpGet(newUrl);
   HttpEntity entity2 = httpClient.execute(get, localContext)
     .getEntity();
   String homePage = EntityUtils.toString(entity2,"UTF-8");
   System.out.println(homePage);
   
   EntityUtils.consume(entity2);
   
   //取formhash值
   String formhash =null;
   if(homePage.indexOf("formhash=") != -1){
    int pos = homePage.indexOf("formhash=");
    formhash = homePage.substring(pos+9, pos+17);
    System.out.println("------formhash="+formhash);
   }
   
   List<NameValuePair> postFormparams = new ArrayList<NameValuePair>();
   postFormparams.add(new BasicNameValuePair("message", "很强的贴子啊,顶一个..."));
   postFormparams.add(new BasicNameValuePair("subject", ""));
   postFormparams.add(new BasicNameValuePair("formhash", formhash));
   UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity(postFormparams,
     "UTF-8");
   
   String pUrl="http://www.xxx.cn/forum.php?mod=post&action=reply&fid=1270&tid=14365186&extra=page%3D1&replysubmit=yes&infloat=yes&handlekey=fastpost";
   HttpPost post3 = new HttpPost(pUrl);
   post3.setEntity(postEntity);
   HttpContext localContext3 = new BasicHttpContext();
   HttpResponse response3 = httpClient.execute(post3, localContext3);
   int statusCode3 = response3.getStatusLine().getStatusCode();
   System.out.println("statusCode3:" + statusCode3);
   System.out.println(EntityUtils.toString(response3.getEntity(),"UTF-8"));
   EntityUtils.consume(response3.getEntity());
  }
 }
 
}

 
用到的jar包如下

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值