您的位置:首页 > 其它

模拟登录新浪微博

2015-09-22 14:17 453 查看
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);

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: