1.工具
httpClient jar包
任意抓包工具 推荐 firefox的 firebug(非常好用)
在使用httpClient进行模拟登录时,有时候会遇到需要输入验证码的情况。而对验证码进行识别的方法既费时效果还不好。这里提供一个取巧的方法,即使用浏览器登录时产生的cookie进行登录。
2.上代码
首先是一个模拟http请求的工具类
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class Http {
static CloseableHttpClient httpclient = null;
static RequestConfig requestConfig = null;
static {
requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BEST_MATCH).build();
httpclient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
}
public static String doGet(String url) {
RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BEST_MATCH).build();
CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
try {
// 创建httpget.
HttpGet httpget = new HttpGet(url);
httpget.addHeader("Cookie", "xxxxxxx");
httpget.addHeader("Accept-Encoding","gzip, deflate");
httpget.addHeader("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
httpget.addHeader("Cache-Control","no-cache");
httpget.addHeader("Connection","keep-alive");
httpget.addHeader("Content-Type","application/json; charset=UTF-8");
httpget.addHeader("Referer","http://www.zhihu.com/");
httpget.addHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0");
System.out.println("executing request " + httpget.getURI());
// 执行get请求.
CloseableHttpResponse response = httpclient.execute(httpget);
try {
// 获取响应实体
HttpEntity entity = response.getEntity();
System.out.println("--------------------------------------");
// 打印响应状态
System.out.println(response.getStatusLine());
if (entity != null) {
return EntityUtils.toString(entity);
}
System.out.println("------------------------------------");
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**以utf-8形式读取*/
public static String doPost(String url,List<NameValuePair> formparams) {
return doPost(url, formparams,"UTF-8");
}
public static String doPost(String url,List<NameValuePair> formparams,String charset) {
RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BEST_MATCH).build();
CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
// 创建默认的httpClient实例.
// CloseableHttpClient httpclient = HttpClients.createDefault();
// 创建httppost
HttpPost httppost = new HttpPost(url);
httppost.addHeader("Cookie", "xxxxxxx");
httppost.addHeader("Accept-Encoding","gzip, deflate");
httppost.addHeader("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
httppost.addHeader("Cache-Control","no-cache");
httppost.addHeader("Connection","keep-alive");
httppost.addHeader("Content-Type","application/json; charset=UTF-8");
httppost.addHeader("Referer","http://www.zhihu.com/");
httppost.addHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0");
UrlEncodedFormEntity uefEntity;
try {
uefEntity = new UrlEncodedFormEntity(formparams, charset);
httppost.setEntity(uefEntity);
CloseableHttpResponse response = httpclient.execute(httppost);
try {
// 打印响应状态
System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
if (entity != null) {
return EntityUtils.toString(entity);
}
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
httppost.addHeader(“Cookie”, “xxxxxxx”); 这里xxxxxx的即为登录时产生的cookie 由于每个浏览器产生的cookie不尽相同,故以xxxxx代替
测试类 这里模拟登录的是知乎
public class Test {
public static void main(String[] args) {
String content = Http.doGet("http://www.zhihu.com/");
System.out.println(content);
}
}
测试结果