众所周知,httpclient可以模拟登陆操作,下面我就来个实例测试一下,顺便也回忆下这个开源工具的使用,直接上例子吧!
package com.pyc.search.crawler.node.login;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class LoginMis {
private CloseableHttpClient httpclient;
private HttpPost httppost;// 用于提交登陆数据
private HttpGet httpget;// 用于获得登录后的页面
private String login_success;// 用于构造上面的HttpGet
public LoginMis() {
httpclient = HttpClients.createDefault();
// mis登陆界面网址
httppost = new HttpPost("http://mis.pyc.com.cn/login.aspx");
}
public void logIn(String name, String password) throws Exception {
httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
String post ="__VIEWSTATE=%2FwEPDwUJNjUwNzE0MTM4ZGRq%2FuXaVF47TNHr5WWnDm%2F6haaRp0gOdDNIQoZp%2FJkyIA%3D%3D&__EVENTVALIDATION=%2FwEWBQLc1Oq3DgLEhISFCwKd%2B7qdDgKC3IeGDAK7q7GGCL0B2uWAzgYaqwKD%2F9eVKMfwK6lpSiVQr9EOO89rWz0w&txtName="+name+"&txtPwd="+password+"&btnLogin=%E7%99%BB+%E5%BD%95";
httppost.setEntity(new StringEntity(post, "utf-8"));
try {
// 提交登录数据
HttpResponse re = httpclient.execute(httppost);
System.out.println("response: "+re);
Header[] h =re.getAllHeaders();
for (Header header : h) {
System.out.println(header.toString());
}
// 获得跳转的网址
Header locationHeader = re.getFirstHeader("Location");
// 登陆不成功
if (locationHeader == null) {
System.out.println("用户 : "+name+"登陆不成功,请确认您的用户名和密码正确...");
System.out.println("用户 : "+name+" \t密码:"+password);
return;
} else// 成功
{
login_success = "http://mis.pyc.com.cn"+locationHeader.getValue();// 获取登陆成功之后跳转链接
System.out.println("用户: "+name+" 登陆成功,之后跳转到的网页网址:" + login_success);
TimeUnit.SECONDS.sleep(3);
System.out.println("用户: "+name+" cookie信息: "+re.getLastHeader("Set-Cookie").getValue());
TimeUnit.SECONDS.sleep(3);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void PrintText(String name) throws IOException {
httpget = new HttpGet(login_success);
HttpResponse re2 = null;
try {
re2 = httpclient.execute(httpget);
// 输出登录成功后的页面
String str = EntityUtils.toString(re2.getEntity());
System.out.println("\n"+name+"首页信息如下:");
System.out.println(str.substring(8250,8400));
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
httppost.abort();
httpget.abort();
httpclient.close();
}
}
public static void main(String[] args) throws Exception {
String name = "xxxxx", password = "xxxxxx";
// 自己的账号,口令
LoginMis lr = new LoginMis();
lr.logIn(name, password);
lr.PrintText(name);
}
}