httpclient模拟登录开心网,然后给指定好友发消息,帮忙看一下问题出在哪里?

爪哇三郎 2014-02-21 01:52:19
帮忙看一下问题出在哪里?我用的是httpclient3.1

class Login {
public static String loginurl = "http://www.kaixin001.com/login/login.php";
static Cookie[] cookies = {};

static HttpClient httpClient = new HttpClient();

public static String getUrlContent(String email, String psw, String url)
throws IOException {

/*
* HttpClientParams httparams = new HttpClientParams();
* httparams.setSoTimeout(30000); httpClient.setParams(httparams);
*/
httpClient.getHostConfiguration().setHost("www.kaixin001.com", 80);

httpClient.getParams().setCookiePolicy(
CookiePolicy.BROWSER_COMPATIBILITY);
System.setProperty("apache.commons.httpclient.cookiespec",
"COMPATIBILITY");

PostMethod PostMethod = new PostMethod(loginurl);
PostMethod.addRequestHeader("Content-Type",
"application/x-www-form-urlencoded; charset=UTF-8");
// NameValuePair ie = new NameValuePair( "User-Agent" ,
// "Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)" );
// NameValuePair homeurl = new NameValuePair( "url" , "/home/" );

NameValuePair Email = new NameValuePair("email", email);// 邮箱
NameValuePair password = new NameValuePair("password", psw);// 密码
// NameValuePair code = new NameValuePair( "code"
// ,"????");//有时候需要验证码,暂时未解决

NameValuePair[] data = { Email, password };
PostMethod.setRequestBody(data);

try {
httpClient.executeMethod(PostMethod);
int statuscode = PostMethod.getStatusCode();
System.out.println(statuscode + "-----------");

cookies = httpClient.getState().getCookies();
System.out.println("==========Cookies============");
int i = 0;
for (Cookie c : cookies) {
System.out.println(++i + ": " + c);
}
httpClient.getState().addCookies(cookies);

// 当state为301或者302说明登陆页面跳转了,登陆成功了
if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY)
|| (statuscode == HttpStatus.SC_MOVED_PERMANENTLY)
|| (statuscode == HttpStatus.SC_SEE_OTHER)
|| (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
// 读取新的 URL 地址
Header header = PostMethod.getResponseHeader("location");
System.out.println("获取到跳转header>>>" + header);
if (header != null) {
String newuri = header.getValue();
if ((newuri == null) || (newuri.equals("")))
newuri = "/";
GetMethod redirect = new GetMethod(newuri);
// ////////////
redirect.setRequestHeader("Cookie", cookies.toString());
httpClient.executeMethod(redirect);
System.out.println("Redirect:"
+ redirect.getStatusLine().toString());
redirect.releaseConnection();

} else
System.out.println("Invalid redirect");
} else {
// 用户名和密码没有被提交,当登陆多次后需要验证码的时候会出现这种未提交情况
System.out.println("用户没登陆");
System.exit(1);
}
} catch (HttpException e) {
// 发生致命的异常,可能是协议不对或者返回的内容有问题
System.out.println("请检查http地址!");
e.printStackTrace();
} catch (IOException e) {
// 发生网络异常
e.printStackTrace();
} finally {
// 释放连接
PostMethod.releaseConnection();
}
return null;
}

public static void sendMsg() throws Exception {
// 登录后发消息
System.out.println("*************发消息***********");

String posturl = "http://www.kaixin001.com/msg/post.php";
PostMethod poster = new PostMethod(posturl);

poster.setRequestHeader("Cookie", cookies.toString());
poster.addRequestHeader("Content-Type",
"application/x-www-form-urlencoded; charset=UTF-8");


NameValuePair uids = new NameValuePair("uids", "89600585");// 发送的好友对象的id
NameValuePair content = new NameValuePair("content", "你好啊!");// 需要发送的信息的内容
NameValuePair texttype = new NameValuePair("texttype", "plain");
NameValuePair send_separate = new NameValuePair("send_separate", "0");
NameValuePair service = new NameValuePair("service", "0");
NameValuePair[] msg = { uids, content,texttype,send_separate,service };

poster.setRequestBody(msg);
httpClient.executeMethod(poster);

InputStream result = poster.getResponseBodyAsStream();
System.out.println(StreamOut(result, "utf8"));
int statuscode = poster.getStatusCode();
System.out.println(statuscode + "-----------");
// 读取新的 URL 地址
// /msg/send.php?flag=1&mid=3195596560
Header header = poster.getResponseHeader("location");
System.out.println("获取到跳转header>>>" + header);
poster.releaseConnection();
}

public static String StreamOut(InputStream txtis, String code)
throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(txtis,
code));
String tempbf;
StringBuffer html = new StringBuffer(100);
while ((tempbf = br.readLine()) != null) {
html.append(tempbf + "\n");
}
return html.toString();

}
}


这里是测试类

public class Send {
public static void main(String[] args) throws Exception {
String username = "11111111@qq.com";
String password = "xxxxxxxxxx";
// 消息发送的action,可更改为别的页面,那么rtnBack返回登录成功后该页面的信息
String url = "http://www.kaixin001.com/";
try {
Login.getUrlContent(username, password,url);// 返回信息发送成功后跳转的页面
Login.sendMsg();//发送消息
} catch (IOException e) {
e.printStackTrace();
}
}
}


现在我也不知道是不是已经登录成功了,执行Login.getUrlContent()的时候返回的header是"/home",这是不是成功了?
还有如果大家要回答的话,最好把运行成功的代码贴上来,不需要引经据典!!!!
...全文
105 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
爪哇三郎 2014-02-21
  • 打赏
  • 举报
回复
结贴了,自己花了2个小时搞定了,呵呵
爪哇三郎 2014-02-21
  • 打赏
  • 举报
回复
竟然没有人!!!
爪哇三郎 2014-02-21
  • 打赏
  • 举报
回复
需要修改一下 loginurl = "https://security.kaixin001.com/login/login_post.php"; NameValuePair Email = new NameValuePair("loginemail", email);// 邮箱 现在登录返回的是 获取到跳转header>>>Location: http://www.kaixin001.com/home/?uid=159231557&s=90 这是正确的,证明已经登录成功了,但是发送消息还是不行,怎么办???

50,534

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧