HttpClient 模拟登录手机版新浪微博
来源:互联网 发布:java命令行参数args 编辑:程序博客网 时间:2024/05/01 11:43
我们要做的就是获取服务器需要的数据,然后用HttpClient模拟浏览器提交。
有几点需要说明一下:
go标签内的href是我们第二次请求的地址,通过这个地址来获取cookie。
用户密码那个field的name属性是随机变化的,需要先获取它然后提交。
下面是整个源代码。
思路:先获取表单的值,然后用POST方法提交。注意添加RequestHeader信息,否则会被服务器拒绝(403)。
/** * 获取手机版微博的cookies * @param username * 微博用户名 * @param password * 微博密码 * @return Cookie[] */ public Cookie[] getMCookies(String username, String password){ Cookie[] cookies = null; HttpClient client; PostMethod post = null; try{ Document doc = Jsoup.connect("http://3g.sina.com.cn/prog/wapsite/sso/login.php").get(); Elements elements = doc.getElementsByTag("postfield"); String randUrl = doc.getElementsByTag("go").first().attr("href"); String pwName = elements.get(1).attr("name"); String vkValue = elements.get(2).attr("value"); String backURL = elements.get(4).attr("value"); String backTitle = elements.get(5).attr("value"); String url = "http://3g.sina.com.cn/prog/wapsite/sso/"+randUrl; post = new PostMethod(url); post.setRequestHeader("User-Agent","Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.77 Safari/537.1"); post.setRequestHeader("Referer", "http://weibo.com/"); post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); post.setParameter("mobile", username); post.setParameter(pwName,password); post.setParameter("vk", vkValue); post.setParameter("remember", "on"); post.setParameter("backURL", backURL); post.setParameter("backTitle", backTitle); post.setParameter("submit", "1"); client = new HttpClient(); client.executeMethod(post); cookies = client.getState().getCookies(); }catch(IOException e){ e.printStackTrace(); }finally{ post.abort(); } return cookies; }
如果用户名密码正确的话,应该就可以获得cookie了。
接下来如果想要访问某些网页只需要带上cookie就行了。
由于新浪的网页可能会变化,所以不保证此方法一直能用,不过大体思路应该是不会变的。
转:http://www.cnblogs.com/e241138/archive/2012/09/16/2687120.html
0 0
- HttpClient 模拟登录手机版新浪微博
- HttpClient 模拟登录手机版新浪微博
- HttpClient 模拟登录手机版新浪微博
- HttpClient 模拟登录Web版新浪微博
- HttpClient 模拟登录Web版新浪微博
- HttpClient 模拟登录Web版新浪微博
- httpclient登录新浪微博手机版Weibo.cn
- httpClient模拟新浪登录
- 新浪微博爬虫模拟登录(爬手机版)
- 模拟登陆手机版新浪微博
- HttpClient模拟登录手机版微博
- 模拟登录新浪微博
- php 新浪通行证登录 新浪微博登录 模拟登录 (浏览器版) 2016
- httpClient4模拟登录新浪微博
- 模拟登录新浪微博(Python)
- scrapy模拟登录新浪微博
- node.js 模拟登录新浪微博
- C# 新浪微博模拟登录
- activeMQ的源码分析 -TCP通讯机制
- Software caused connection abort: recv failed
- sqlite3树形结构遍历效率对比测试
- 使用httpclient、htmlcleaner 、xpath 采集新浪微博3G站点数据
- JVM 优化经验总结
- HttpClient 模拟登录手机版新浪微博
- ImageView.ScaleType设置图解
- HttpClient 模拟登录Web版新浪微博
- Soap请求获取指定城市的天气情况并在天地图API Map中显示
- ajax 初探
- Fiddler 实现手机的抓包
- hdu 2089 不要62(基础)
- org.hibernate.HibernateException: connnection proxy not usable after transaction
- jQuery异步框架探究1:jQuery._Deferred方法