当前位置:编程学习 > JAVA >>

模拟登陆人人网并留言遇到问题

今天做了个模拟登陆人人网的小程序登录能够正常登陆但是留言时就不行了希望有高手指点我一下..
我将我的代码贴一下:
package com.xiaoqiang.domain;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.UrlEncodedFormEntity;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;

public class DoMain {
//登陆校内网--成功
public static void main(String args[])throws Exception{
HttpClient client =new DefaultHttpClient();
client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
System.out.println("访问地址"+"http://www.renren.com/PLogin.do");
HttpPost post=new HttpPost("http://www.renren.com/PLogin.do");
Header head1=new BasicHeader("Accept","image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/QVOD, application/QVOD, */*");
Header head2=new BasicHeader("Referer","http://www.renren.com/PLogin.do");
Header head3=new BasicHeader("Accept-Language","zh-cn");
Header head4=new BasicHeader("Content-Type","application/x-www-form-urlencoded");
Header head5=new BasicHeader("UA-CPU","x86");
Header head6=new BasicHeader("Accept-Encoding","gzip, deflate");
Header head7=new BasicHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727)");
Header head8=new BasicHeader("Host","www.renren.com");
Header head9=new BasicHeader("Connection","Keep-Alive");
Header head10=new BasicHeader("Cache-Control","no-cache");
Header head11=new BasicHeader("Cookie","ick=c7782bec-a19a-4177-a4d8-ea2517415e6c; _r01_=1; _de=25733BD8775D04BB608BA0DF927B991F5212E40F3D18115C");
post.addHeader(head1);
post.addHeader(head2);
post.addHeader(head3);
post.addHeader(head4);
post.addHeader(head5);
post.addHeader(head6);
post.addHeader(head7);
post.addHeader(head8);
post.addHeader(head9);
post.addHeader(head10);
post.addHeader(head11);
NameValuePair[] namevaluepair=new NameValuePair[]{
new BasicNameValuePair("email","**********@163.com"),
new BasicNameValuePair("password","05***********32"),
new BasicNameValuePair("origURL","http%3A%2F%2Fwww.renren.com%2Fhome"),
new BasicNameValuePair("domain","renren.com"),
new BasicNameValuePair("formName",""),
new BasicNameValuePair("method",""),
new BasicNameValuePair("isplogin","true"),
new BasicNameValuePair("submit","%E7%99%BB%E5%BD%95")
};
post.setEntity(new UrlEncodedFormEntity(namevaluepair, "US-ASCII"));
HttpResponse response=client.execute(post);
HttpEntity entity=response.getEntity();
// System.out.println("执行回复 : " + response.getStatusLine());
if(entity!=null){
BufferedReader reader = new BufferedReader(new InputStreamReader(
entity.getContent()));
String line=null;
while((line=reader.readLine())!=null){
System.out.println(line);
}
reader.close();
}
System.out.println("提交后的Cookies :");
Cookie[] cookies = ((AbstractHttpClient) client).getCookieStore()
.getCookies();
if (cookies.length == 0) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.length; i++) {
System.out.println("- " + cookies[i].toString());
}
}
//点击留言--成功
HttpGet getl=new HttpGet("http://gossip.renren.com/?origin=103");
Header headg1=new BasicHeader("Accept","image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/QVOD, application/QVOD, */*");
Header headg2=new BasicHeader("Accept-Language","zh-cn");
Header headg3=new BasicHeader("Referer","http://www.renren.com/home?id=225417436");
Header headg4=new BasicHeader("UA-CPU","x86");
Header headg5=new BasicHeader("Accept-Encoding","gzip, deflate");
Header headg6=new BasicHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727)");
Header headg7=new BasicHeader("Host","gossip.renren.com");
Header headg8=new BasicHeader("Connection","Keep-Alive");
Header headg9=new BasicHeader("Cookie","_r01_=1; depovince=SD; p=5e630e6448f0e070735a1754c7a647ad6; t=aae31f83b4f44d93b317c5b72875f46f6; societyguester=aae31f83b4f44d93b317c5b72875f46f6; id=225417436; xnsid=febf2290; kl=kl_225417436; loginfrom=null; JSESSIONID=abcbePTLQ4sZs4kEDqb0s");
getl.addHeader(headg1);
getl.addHeader(headg2);
getl.addHeader(headg3);
getl.addHeader(headg4);
getl.addHeader(headg5);
getl.addHeader(headg6);
getl.addHeader(headg7);
getl.addHeader(headg8);
getl.addHeader(headg9);
response=client.execute(getl);
entity=response.getEntity();
// System.out.println("执行回复 : " + response.getStatusLine());
if(entity!=null){
BufferedReader reader = new BufferedReader(new InputStreamReader(
entity.getContent()));
String line=null;
while((line=reader.readLine())!=null){
System.out.println(line);
}
reader.close();
}
System.out.println("提交后的Cookies :");
cookies=null;
cookies = ((AbstractHttpClient) client).getCookieStore()
.getCookies();
if (cookies.length == 0) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.length; i++) {
System.out.println("- " + cookies[i].toString());
}
}
//开始留言
HttpPost postl=new HttpPost("http://gossip.renren.com/gossip.do");
Header headp1=new BasicHeader("Accept","*/*");
Header headp2=new BasicHeader("Accept-Language","zh-cn");
Header headp3=new BasicHeader("Referer","http://gossip.renren.com/?origin=103");
Header headp4=new BasicHeader("Content-Type","application/x-www-form-urlencoded");
Header headp5=new BasicHeader("UA-CPU","x86");
Header headp6=new BasicHeader("Accept-Encoding","gzip, deflate");
Header headp7=new BasicHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727)");
Header headp8=new BasicHeader("Host","gossip.renren.com");
Header headp9=new BasicHeader("Connection","Keep-Alive");
Header headp10=new BasicHeader("Cache-Control","no-cache");
Header headp11=new BasicHeader("Cookie","_r01_=1; depovince=SD; p=5e630e6448f0e070735a1754c7a647ad6; t=aae31f83b4f44d93b317c5b72875f46f6; societyguester=aae31f83b4f44d93b317c5b72875f46f6; id=225417436; xnsid=febf2290; kl=kl_225417436; loginfrom=null; _urm_225417436=21; JSESSIONID=abcbePTLQ4sZs4kEDqb0s");
// Header headp12=new BasicHeader("X_REQUESTED_WITH", "XMLHttpRequest");
postl.addHeader(headp1);
postl.addHeader(headp2);
postl.addHeader(headp3);
postl.addHeader(headp4);
postl.addHeader(headp5);
postl.addHeader(headp6);
postl.addHeader(headp7);
postl.addHeader(headp8);
postl.addHeader(headp9);
postl.addHeader(headp10);
postl.addHeader(headp11);
// postl.addHeader(headp12);

NameValuePair[] namevaluepairp=new NameValuePair[]{
new BasicNameValuePair("body","..........."),
new BasicNameValuePair("id","225417436"),
new BasicNameValuePair("cc","225417436"),
new BasicNameValuePair("headUrl",""),
new BasicNameValuePair("largeUrl",""),
new BasicNameValuePair("requestToken","1638050254"),
new BasicNameValuePair("only_to_me","0"),
new BasicNameValuePair("color",""),
new BasicNameValuePair("ref","http://gossip.renren.com/&mode=&requestToken=1638050254")
};
postl.setEntity(new UrlEncodedFormEntity(namevaluepairp,"US-ASCII"));
response=client.execute(postl);
entity=response.getEntity();
System.out.println("执行回复 : " + response.getStatusLine());
if(entity!=null){
BufferedReader reader = new BufferedReader(new InputStreamReader(
entity.getContent()));
String line=null;
while((line=reader.readLine())!=null){
System.out.println(line);
}
reader.close();
}
System.out.println("提交后的Cookies :");
cookies=null;
cookies = ((AbstractHttpClient) client).getCookieStore()
.getCookies();
if (cookies.length == 0) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.length; i++) {
System.out.println("- " + cookies[i].toString());
}
}
以上是我的程序代码...希望有师傅来指点一下..谢谢.. --------------------编程问答-------------------- 出现了什么问题勒 --------------------编程问答-------------------- 你用个Httpwatch抓包看下,要把所有有关的Header信息设置进出
补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,