模拟登录新浪微博

package getLog;

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.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.AbstractHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

@SuppressWarnings("deprecation")
public class HttpLogin {
	private String loginurl="http://login.weibo.cn/login/";
	private String profileurl="http://weibo.cn/";
	private AbstractHttpClient client=new DefaultHttpClient();
	List<NameValuePair> paramList=new ArrayList<NameValuePair>();
	String pwdname=null;
	String action=null;
	String gsid=null;
public void login(String username,String password) throws ClientProtocolException, IOException{
	HttpGet req=new HttpGet(loginurl);
	req.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; rv:16.0) Gecko/20100101 Firefox/16.0");
	HttpResponse res=client.execute(req);
	HttpEntity entity=res.getEntity();
	if(entity!=null){
		String content=EntityUtils.toString(entity,"utf-8");
		System.out.println(content);
		Document doc=Jsoup.parse(content);
		Elements inputs=doc.getElementsByTag("input");
		Element form=doc.getElementsByTag("form").get(0);
		for(int i=0;i<inputs.size();i++){
			Element input=inputs.get(i);
			if(input.attr("name").equalsIgnoreCase("password")||input.attr("name").startsWith("password_"))
				pwdname=input.attr("name");
				if(input.attr("type").equals("hidden"))
					paramList.add(new BasicNameValuePair(input.attr("name"),input.attr("value")));
		}
		action=form.attr("action");
		paramList.add(new BasicNameValuePair("mobile",username));
		paramList.add(new BasicNameValuePair(pwdname,password));
		paramList.add(new BasicNameValuePair("submit","登录"));
		UrlEncodedFormEntity formEntity=new UrlEncodedFormEntity(paramList,"utf-8");
		HttpPost post=new HttpPost(loginurl+action);
		post.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; rv:16.0) Gecko/20100101 Firefox/16.0");
		post.setHeader("Referer",loginurl);
		post.setHeader("Content-Type","application/x-www-form-urlencoded");
		post.setEntity(formEntity);
		HttpResponse res1=client.execute(post);
		HttpEntity entity1=res1.getEntity();
		if(entity1!=null)
			entity1.consumeContent();
		String redirectStrParams=res1.getLastHeader("Location").getValue().split("\\?")[1];
		
		System.out.println(redirectStrParams);
		
		String[] paramArray=redirectStrParams.split("&");
		for(int i=0;i<paramArray.length;i++){
			if(paramArray[i].startsWith("g=")){
				gsid="gsid="+paramArray[i].split("=")[1];
				System.out.println(gsid);
				break;
			}
		}
		HttpGet get=new HttpGet(profileurl+"?"+gsid);
		get.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; rv:16.0) Gecko/20100101 Firefox/16.0");
		get.setHeader("Referer",loginurl);
		HttpResponse res2=client.execute(get);
		entity=res2.getEntity();
		if(entity!=null){
		 content=EntityUtils.toString(entity,"utf-8");
		}
	System.out.println(content);
		
	}
}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值