前言
当我们在实际开发中往往需要用到第三方的数据,比如天气数据、彩票中奖信息数据等等,想通过程序抓取对应的数据信息,我们可以用到Apache旗下的httpclient来解决。虽然在 JDK 的 java net包中已经提供了访问 HTTP 协议的基本功能,但是对于大部分应用程序来说,JDK 库本身提供的功能还不够丰富和灵活。在本篇中httpclient的版本号为4.5.2,将通过如下几个案例来说明httpclient的使用场景,具体的代码请前往我的github。
- 使用httpclient获取体彩最近一期的开奖结果
- 模拟自动登录
- 抓取一个第三方需要登录授权的接口数据
代码案例
添加maven依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.2</version>
<scope>provided</scope>
</dependency>
获取体彩开奖结果案例
- 请求参数构造数据处理
说明:获取体彩开奖结果的接口为:http://www.lottery.gov.cn/tz_kj.json
/**
* 根据体彩类型查询当天的开奖结果信息
*/
public static LotteryResult getTodayLotteryResult(LotteryType lotteryType) {
logger.info("getTodayLotteryResult==>enter,lotteryType:{}", lotteryType);
String doGet = HttpClientUtil.doGet(CommonBusinessConstant.GOV_LOTTERY_RESULT_URI);
logger.info("doGet:{}", doGet);
List<Map> result = gson.fromJson(doGet, new TypeToken<List<Map>>() {
}.getType());
logger.info("result:{}", result);
Map map = result.get(0);
logger.info("Map:{}", map);
logger.info("Map:{}", gson.toJson(map));
if (CollectionUtils.isEmpty(map)) {
return null;
}
LotteryResult lotteryResult = gson.fromJson(gson.toJson(map.get(lotteryType.type)), LotteryResult.class);
if (lotteryResult == null) {
return null;
}
lotteryResult.type = lotteryType.type;
logger.info("体彩:{}开奖结果为:{}", lotteryType, lotteryResult);
return lotteryResult;
}
- 使用httpclient封装通信
/**
* <p>使用get方式访问一个地址
*/
public static String doGet(String uri) {
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
HttpGet get = new HttpGet(uri);
try {
response = httpClient.execute(get);
return dealResponse(response, uri);
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭连接,释放资源
shutDownResoure(httpClient, response);
}
return null;
}
/**
* 处理httpclient返回的结果
* @param res
* @param uri
* @return
* @throws Exception
*/
private static String dealResponse(CloseableHttpResponse res,String uri) throws Exception{
int status = res.getStatusLine().getStatusCode();
logger.info("status:{}", status);
if (HttpStatus.SC_OK == status) {
logger.info("访问:{}接口成功!",uri);
HttpEntity entityLogin = res.getEntity();
if (entityLogin != null) {
String responseData = EntityUtils.toString(entityLogin, CommonBusinessConstant.UTF_8);
logger.info("responseData:{}", responseData);
return responseData;
}
} else {
logger.error("访问:{}接口失败!", uri);
throw new BaseException(CommonExceptionEnums.INTERNET_CONNECTION_FAIL);
}
return null;
}
/**
* 关闭资源
* @param httpClient
* @param response
*/
private static void shutDownResoure(CloseableHttpClient httpClient, CloseableHttpResponse response) {
try {
if (response != null) {
response.close();
}
if (httpClient != null) {
httpClient.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
- 单元测试
@Test
public void testHttpClient() {
logger.info("DemoApplicationTests==>开始测试doGet");
LotteryResult todayLotteryResult =
BusinessUtil.getTodayLotteryResult(LotteryType.PLS);
logger.info("lotteryResult==>:{}", gson.toJson(todayLotteryResult));
logger.info("DemoApplicationTests==>测试doGet成功!");
}
- 测试结果
先模拟自动登录,然后获取一个需要登录接口的数据
- 这里的登录是指简单的登录,针对需要图片验证码、短信验证码的登录接口不在本文讨论的范围内。而这些复杂的接口也是为了防止机器自动登录。
- 需要说明的是,通过httpclient模拟浏览器登录,最重要的就是cookie信息的携带,经过测试需要在登录之前使用一个get接口访问获取必要的cookie信息,然后通过post请求登录接口进一步完善cookie信息,最后就是目标接口了。
- 代码案例
/**
* 通过httpclient访问一个需要登录的接口
* @param beforeLoginUri 登录前某一个get请求的uri,为了获取cookie
* @param loginUri 登录的uri
* @param loginPara 登录需要的参数(简单登录,有图片验证码短信验证码的接口不在此范围)
* @param targetUri 需要获取资源的目标接口
*/
public static void getAuthorizedResource(String beforeLoginUri, String loginUri, List<BasicNameValuePair> loginPara, String targetUri) throws Exception{
logger.info("getAuthorizedResource enter!");
if (StringUtils.isAnyBlank(beforeLoginUri, loginUri, targetUri) || CollectionUtils.isEmpty(loginPara)) {
throw new BaseException(CommonExceptionEnums.INVALID_PARAMETER);
}
CloseableHttpClient httpClient = null;
CloseableHttpResponse res = null;
try {
// 全局请求设置
RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build();
// 创建cookie store的本地实例
CookieStore cookieStore = new BasicCookieStore();
// 创建HttpClient上下文
HttpClientContext context = HttpClientContext.create();
context.setCookieStore(cookieStore);
// 创建一个HttpClient实例
httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig)
.setDefaultCookieStore(cookieStore).build();
//1访问主页获取一些必要的cookie信息
HttpGet first = new HttpGet(beforeLoginUri);
res = httpClient.execute(first, context);
logger.info("登录之前抓取的cookie信息为");
for (Cookie c : cookieStore.getCookies()) {
logger.info("Cookie:{}--:{}",c.getName(),c.getValue());
}
String beforeLoginUriRes = dealResponse(res, beforeLoginUri);
logger.info("访问:{}接口返回的结果为:{}",beforeLoginUri,beforeLoginUriRes);
res.close();
//2访问登录接口获取token信息
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(loginPara, Consts.UTF_8);
entity.setContentType("application/x-www-form-urlencoded");
// 创建一个post请求
HttpPost post = new HttpPost(loginUri);
// 注入post数据
post.setEntity(entity);
post.setConfig(globalConfig);
res = httpClient.execute(post, context);
String dealResponse = dealResponse(res, loginUri);
logger.info("访问:{}接口返回的结果为:{}",loginUri,dealResponse);
res.close();
logger.info("登录成功后的cookie信息!");
for (Cookie c : cookieStore.getCookies()) {
logger.info("Cookie====>{}--{}", c.getName(), c.getValue());
}
//3访问目标接口
HttpGet newGet = new HttpGet(targetUri);
res = httpClient.execute(newGet, context);
String targetRes = dealResponse(res, targetUri);
logger.info("访问:{}接口返回的结果为:{}",targetUri,targetRes);
}finally {
// 关闭连接,释放资源
shutDownResoure(httpClient, res);
}
}
- 单元测试案例
@Test
public void testSimulateLogin() throws Exception{
BasicNameValuePair pair1 = new BasicNameValuePair("mobile", "134****1325");
BasicNameValuePair pair2 = new BasicNameValuePair("password", RsaPcUtil.Encrypt("****"));
List<BasicNameValuePair> loginPara=new LinkedList<>();
loginPara.add(pair1);
loginPara.add(pair2);
String beforeLoginUri = "https://www.zyfax.cn/CMSWeb/Floating/getIndexFloating.json";
String loginUri = "https://www.zyfax.cn/UserWeb/login.json";
String targetUri = "https://www.zyfax.cn/VIPWeb/score/signIn.json";
HttpClientUtil.getAuthorizedResource(beforeLoginUri, loginUri, loginPara, targetUri);
}
- 测试结果
总结
上面的两个代码案例已经显示出了httpclient的使用详情,篇幅有限,更加完善的代码请前往我的github:https://github.com/tianmlin19/