模拟登录
1、session方式
package com.crawinfo.httpclient;
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
public class HttpClientTest {
/**
* @param args
* @throws IOException
* @throws HttpException
*/
public static void main(String[] args) throws HttpException, IOException {
HttpClient httpclient=new HttpClient();//创建一个客户端,类似打开一个浏览器
GetMethod getMethod=new GetMethod("受保护的地址");
PostMethod postMethod = new PostMethod("登录url");
NameValuePair[] postData = new NameValuePair[2];
postData[0] = new NameValuePair("loginName", "***");
postData[1] = new NameValuePair("loginPswd", "**");
postMethod.addParameters(postData);
int statusCode=httpclient.executeMethod(postMethod);//回车——出拳!
statusCode= httpclient.executeMethod(getMethod);
System.out.println("response1=" + postMethod.getResponseBodyAsString());//察看拳头命中情况,可以获得的东西还有很多,比如head, cookies等等
System.out.println("response2=" + getMethod.getResponseBodyAsString());//察看拳头命中情况,可以获得的东西还有很多,比如head, cookies等等
getMethod.releaseConnection();
postMethod.releaseConnection();//释放,记得收拳哦
}
}
二、cookie方式
package com.crawinfo.rencai;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
public class CrawTest {
static Cookie[] cookies=new Cookie[2];
public static void login() throws Exception {
cookies[0] = new Cookie("抓取信息网址", "SessionId",
"0", "/", -1, false);
cookies[1] = new Cookie("抓取信息网址", "otherproperty",
"xxx", "/", -1, false);
}
public static String getRes(String path)throws Exception{
String res=null;
HttpClient client=new HttpClient();
HttpMethod method=new GetMethod(path);
client.getState().addCookies(cookies);
client.executeMethod(method);
if(method.getStatusCode()==200){
res=method.getResponseBodyAsString();
cookies=client.getState().getCookies();
}
method.releaseConnection();
return res;
}
public static void main(String[] args) throws Exception {
CrawTest.login();
String info = CrawTest.getRes("抓取信息网址");
System.out.println("info="+info);
}
}