本文共 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参数 Listnvps2 = 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/