(一) maven环境下引入必备jar
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
(二) 使用httpClient 获取登录的cookie并携带cookie发起请求
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.HttpException;
org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
httpClient.getHostConfiguration().setProxy("localhost",8888);
PostMethod postMethod = new PostMethod("loginUrl");
PostMethod method = new PostMethod("登录后请求的地址");
NameValuePair[] data = { new NameValuePair("key","value") };
postMethod.setRequestBody(data);
httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
Cookie[] cookies = httpClient.getState().getCookies();
StringBuffer tmpcookies = new StringBuffer();
for (Cookie c : cookies) {
tmpcookies.append(c.toString() + ";");
}
method.setRequestHeader("cookie",tmpcookies.toString());
int executeMethod = httpClient.executeMethod(method);
org.apache.commons.httpclient.Header[] headers = method.getResponseHeaders();
InputStream stream = method.getResponseBodyAsStream();
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
StringBuffer stringBuffer = new StringBuffer();
String str1= "";
while((str1 = br.readLine()) !=null){
stringBuffer.append(str1);
}
String sb=stringBuffer.toString();
String result = new String(sb.getBytes(),"UTF-8");
JSONArray fromObject = JSONArray.fromObject(result);
JSONObject json = fromObject.getJSONObject(0);
logger.info(result);
(三)使用时遇到的一些坑
- 使用postMethod访问登陆的url获取的cookie有问题,导致后期通过这个cookie请求数据失效,后来通过fiddler抓包看到了正确的登陆请求地址。fiddler大法好。
- 在postMethod请求携带cookie信息时候,之前使用如下方式意图携带cookie访问,实测无效。
BasicCookieStore cookieStore = new BasicCookieStore();
BasicClientCookie cookie = new BasicClientCookie("key","value");
cookieStore.addCookie(cookie);
httpClient.setCookieStore(cookieStore);
httpClient.execute("method");
解决发起post请求时候中文乱码问题
method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");
public class UTF8PostMethod extends PostMethod {
public UTF8PostMethod(String url){
super(url);
}
@Override
public String getRequestCharSet() {
return "UTF-8";
}
}