50,534
社区成员
发帖
与我相关
我的任务
分享
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();
}
}
}