标签:图书馆 模拟登陆 httpclient
最近想做一个图书馆的客户端,由于学校没有提供API,只能模拟登陆然后爬取数据了。
首先要解决的就是登陆问题,其实会了之后并不难,我在此竟然耗费了两天……都是一些细节问题。
采用HTTPclient模拟请求,需要注意:
<span style="font-size:14px;">package com.ali.login;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
public class LibraryUtil {
private static final String baseUrl = "http://innopac.lib.xjtu.edu.cn";
private static String POST_URL = "http://innopac.lib.xjtu.edu.cn/patroninfo*chx";
private static String KEY_CODE = "code";
private static String KEY_PIN = "pin";
private static String KEY_Submit_x = "submit.x";// 26
private static String KEY_Submit_y = "submit.y";// 20
// The HttpClient is used in one session
private static HttpResponse response;
private static HttpClient httpclient = null;
private static String resultHtml = null;
/**
* @param args
*/
public static void main(String[] args) {
boolean isConn = login("2111******", "********");
if (isConn) {
System.out.println(resultHtml);
}
}
/**
* 登陆
*
*/
public static boolean login(String userName, String password) {
HttpGet httpget = null;
HttpPost httppost = null;
try {
httpclient = new DefaultHttpClient(); // 看作是浏览器
BasicHttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
HttpConnectionParams.setSoTimeout(httpParams, 10000);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair(KEY_CODE, userName));
nameValuePairs.add(new BasicNameValuePair(KEY_PIN, password));
nameValuePairs.add(new BasicNameValuePair(KEY_Submit_x, Integer
.toString(50)));
nameValuePairs.add(new BasicNameValuePair(KEY_Submit_y, Integer
.toString(25)));
httppost = new HttpPost(POST_URL);
httppost.setHeader("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
httppost.setHeader("Accept-Language",
"zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
httppost.setHeader("Accept-Encoding", "gzip, deflate");
httppost.setHeader("Referer",
"http://innopac.lib.xjtu.edu.cn/patroninfo*chx");
httppost.setHeader("Connection", "keep-alive");
httppost.setHeader("Content-Type",
"application/x-www-form-urlencoded");
httppost.setHeader("Host", "innopac.lib.xjtu.edu.cn");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);// 发送post请求
int code = response.getStatusLine().getStatusCode();
System.out.println(response.getStatusLine());
// 200说明密码或用户名错误,302则说明正常登陆
if (code == 200) {
System.out.println("用户名或密码错误,请重新登陆");
return false;
} else if (code == 302) {
System.out.println("登陆成功,跳转中...");
String location = response.getHeaders("Location")[0].getValue();
System.out.println(location);
httppost.abort();
httpget = new HttpGet(baseUrl + location);
response = httpclient.execute(httpget);// 发送get请求
code = response.getStatusLine().getStatusCode();
System.out.println(response.getStatusLine());
if (code == 200) {
if (response != null) {
resultHtml = EntityUtils.toString(response.getEntity(),
HTTP.UTF_8);
}
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
httpget.abort();
httpclient.getConnectionManager().shutdown();
}
return false;
}
}
</span>标签:图书馆 模拟登陆 httpclient
原文地址:http://blog.csdn.net/leokelly001/article/details/42006009