HttpClient4模拟登录回贴
经过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());}}}
页:
[1]