模拟登录
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();//释放,记得收拳哦
}
}