用HttpClient 4.X来实现获取登录的页面比较容易,不需要获取Cookie值,默认浏览器HttpClient对象会自动保存Cookie信息,只要是用的一个HttpClient对象,在登录后,就可以获得想要的页面了。
我先用了Post方法,提交表单,模拟登录,后用了Get方法,获取登录后的信息。
这里要注意的是,Post方法用完后,需要释放HttpPost,否则无法再用HttpClient.execute(httpGet);
代码:
package com.llwwlql.spider.contest;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.Cookie;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.util.EntityUtils;
import com.llwwlql.bean.Contest;
public class HduLogin {
private HttpClient httpClient = new DefaultHttpClient();
private String loginUrl = "http://acm.hdu.edu.cn/diy/contest_login.php?action=login&cid=";
private Contest contest = new Contest();
private HttpHost host = new HttpHost("acm.hdu.edu.cn");
private String baseUrl = "http://acm.hdu.edu.cn/diy/";
private String location;
private void doGet() throws IOException {
// TODO Auto-generated method stub
StringBuffer strResult = new StringBuffer();
location = "http://acm.hdu.edu.cn/diy/contest_ranklist.php?page=1&cid=" + 30926;
try {
HttpGet httpget = new HttpGet(location);
HttpResponse response = httpClient.execute(host, httpget);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
// 获取网页源码信息
strResult.append(EntityUtils.toString(entity, "GB2312"));
System.out.println(strResult.toString());
} else {
System.out.println(response.getStatusLine().getStatusCode());
System.out.println("登录失败!");
}
httpget.abort();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void doPost() throws IOException {
// TODO Auto-generated method stub
// int contestId = contest.getId();
int contestId = 30926;
loginUrl = loginUrl + contestId;
try {
HttpPost httpost = new HttpPost(loginUrl);
List<NameValuePair> nvp = new ArrayList<NameValuePair>();
nvp.add(new BasicNameValuePair("password", 密码));
httpost.setEntity(new UrlEncodedFormEntity(nvp, Charset
.forName("gb2312")));
HttpResponse response = httpClient.execute(host, httpost);
if (response.getStatusLine().getStatusCode() == 302) {
httpost.abort();
this.doGet();
} else {
System.out.println("登录HDUContest失败!");
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
HduLogin hdu = new HduLogin();
hdu.doPost();
}
}