模拟登录新浪微博
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); } } }
相关文章推荐
- PHP:Linux/Ubuntu15.04搭建LAMP开发环境图解教程
- Android的selector,背景选择器
- PLSQL Developer连接64位Oracle
- 清除作业列表SQL
- 第二次作业——结对项目之需求分析与原型模型设计
- ASP.NET MVC 3 初认知
- 用命令查看端口占用情况 netstat -ano
- 使用Eclipse Memory Analyzer进行内存泄漏分析三部曲
- win7 64位纯净版图标变成了一样该怎么办?win7旗舰版图标变成一样的解决方法
- Windows不能在本地计算机启动MongoDB,错误代码 100
- 紫金桥软件与LG K120S的RS485通讯
- java基本类型数组初始化
- 常用的正则表达式_总结
- 1001 The Cats' Feeding Spots (2015年北京赛区网络赛A题)
- 第八节 命令执行顺序控制与管道
- Java中的单例模式的总结
- 使用Jenkins配置Git+Maven的自动化构建
- win7桌面图标出现蓝色底的白色问号解决方法
- C++Primer第五版 练习11.3(解答)
- bzoj4160: [Neerc2009]Exclusive Access 2