博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用Httpclient4.4+Jsoup登录3G版人人网
阅读量:4290 次
发布时间:2019-05-27

本文共 5968 字,大约阅读时间需要 19 分钟。

package com.chenjazz.testMobilRenren;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.apache.http.Header;import org.apache.http.HttpEntity;import org.apache.http.NameValuePair;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;import org.jsoup.Jsoup;import org.jsoup.nodes.Document;import org.jsoup.select.Elements;/** * @ClassName: Test * @Description:  * @author chenjazz@foxmail.com * @date 2015年6月8日 上午9:55:02  */public class Test {	public static void main(String[] args) throws ClientProtocolException, IOException, InterruptedException {		CloseableHttpClient httpclient = HttpClients.createDefault();		String email="你的人人email账号";		String password="你的密码";		try {			// 1.到登陆界面--wap.renren.com			System.err.println("步骤1:登陆http://wap.renren.com,获取lbskey...");			HttpGet httpGet1 = new HttpGet("http://wap.renren.com");			httpGet1.setHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3");			CloseableHttpResponse response1 = httpclient.execute(httpGet1);			HttpEntity entity1 = response1.getEntity();// 得到响应正文			// Jsoup的用法:详见http://www.open-open.com/jsoup/			Document doc1 = Jsoup.parse(EntityUtils.toString(entity1));			Elements el1 = doc1.select("input[name=lbskey]");			String key = el1.attr("value").toString();			System.out.println(" "+el1.attr("value").toString());			// 释放实体			EntityUtils.consume(entity1);			// 释放响应			response1.close();				        // 2--------post登陆请求			System.err.println("步骤2:向服务器发起post请求...");			HttpPost httpPost2 = new HttpPost("http://3g.renren.com/login.do?autoLogin=true&");						httpPost2.setHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3");			//httpPost2.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0");			// 封装post参数			List
nvps2 = new ArrayList
(); nvps2.add(new BasicNameValuePair("appid", "")); nvps2.add(new BasicNameValuePair("c", "")); nvps2.add(new BasicNameValuePair("email", email)); nvps2.add(new BasicNameValuePair("lbskey", key));// 1中得到的key nvps2.add(new BasicNameValuePair("login", "登陆")); nvps2.add(new BasicNameValuePair("origURL", "")); nvps2.add(new BasicNameValuePair("password", password)); nvps2.add(new BasicNameValuePair("pq", "")); nvps2.add(new BasicNameValuePair("ref", "http://m.renren.com/q.do?null")); httpPost2.setEntity(new UrlEncodedFormEntity(nvps2)); CloseableHttpResponse response2 = httpclient.execute(httpPost2); System.out.println(" 状态-----"+response2.getStatusLine()); /*所有头信息 for(Header h:response2.getAllHeaders()){ System.out.println(h.toString()); }*/ //获取响应头的Location属性值---跳转地址 Header location_h = response2.getFirstHeader("Location"); String loc=location_h.getValue(); System.out.println(" 跳转地址为"+loc); response2.close(); //----------3跳转后------ System.err.println("步骤3:正在跳转..."); HttpGet get3 = new HttpGet(loc.trim()); get3.setHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3"); CloseableHttpResponse response3 = httpclient.execute(get3); System.out.println(" 状态is-----"+response3.getStatusLine()); HttpEntity entity3=response3.getEntity(); Document doc3=Jsoup.parse(EntityUtils.toString(entity3));//主页 Elements url=doc3.select("form"); String url3=url.attr("action"); System.out.println("发状态的url为:-----"+url3);//发状态的url //获取发状态的参数 String _rtk=doc3.select("input[name=_rtk]").attr("value"); String sour=doc3.select("input[name=sour]").attr("value"); String loginbybm=doc3.select("input[name=loginbybm]").attr("value"); //String status=doc3.select("input[name=status]").attr("value"); System.out.println(_rtk+sour+loginbybm); EntityUtils.consume(entity3); response3.close(); Thread.sleep(3000); //4-----------------------发状态---------------------- System.err.println("步骤4:发状态"); HttpPost post4 = new HttpPost(url3);//忘写地址,活该 post4.setHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3"); List
nvps4 = new ArrayList
(); nvps4.add(new BasicNameValuePair("_rtk", _rtk)); nvps4.add(new BasicNameValuePair("loginbybm", "")); nvps4.add(new BasicNameValuePair("pid", "")); nvps4.add(new BasicNameValuePair("sour", sour));// String st=new String("今天心情真好!!!!!".getBytes(),"UTF-8"); nvps4.add(new BasicNameValuePair("status", st)); nvps4.add(new BasicNameValuePair("update", "发布")); post4.setEntity(new UrlEncodedFormEntity(nvps4)); System.out.println(post4); CloseableHttpResponse response4 = httpclient.execute(post4);//????????????? System.out.println("-----"); //获取响应头的Location属性值---跳转地址 Header location_h4 = response4.getFirstHeader("Location"); String loc4=location_h4.getValue(); System.out.println(" 发状态跳转地址---"+loc4); response4.close(); //5------------------发状态,跳转-------------------- System.err.println("步骤5:正在跳转..."); HttpGet get5 = new HttpGet(loc4.trim()); get3.setHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3"); CloseableHttpResponse response5 = httpclient.execute(get5); response5.close(); System.err.println("发送成功!"); } finally { httpclient.close(); } }}

转载地址:http://ijhgi.baihongyu.com/

你可能感兴趣的文章
MYSQL sql 语句性能分析
查看>>
C++操作Redis数据库
查看>>
python yield用法
查看>>
python pipe模块用法
查看>>
安装完 MySQL 后必须调整的 10 项配置
查看>>
开发者必备的 12 个 JavaScript 库
查看>>
http错误码
查看>>
python 多线程
查看>>
sipp命令 各参数含义
查看>>
搜集的动植物分类、检索网站
查看>>
ffmpeg源码分析之媒体打开过程
查看>>
Ubuntu/centos/redhat/SUSE sipp安装(带rtp支持,3.5.1版本)
查看>>
周鸿祎:很多程序员聪明,但我一看就知道他不会成功
查看>>
编译程序遇到问题 relocation R_X86_64_32 against `.rodata' can not be used when making a shared object;
查看>>
Const指针 、 指向const的指针 、引用、指针
查看>>
GDB调试命令
查看>>
常见数据类型的字节数
查看>>
gcc/g++ 编译常见问题
查看>>
【设计模式】 工厂模式实例 C++ , 利用工厂模式写一个计算器
查看>>
opencv
查看>>