java使用get请求调用第三方接口

String userAgent = "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.87 Safari/537.36";
String url = "https://xxx?token=xxx&pn=" +pn +"&rn=" + 
rn + "&app_id=" + app_id;
String result = null;
CloseableHttpResponse res = null;
try {
	CloseableHttpClient httpclient = HttpClients.createDefault();
	HttpGet httpGet = new HttpGet(url);
	httpGet.setHeader("User-Agent", userAgent);
	res = httpclient.execute(httpGet);
	HttpEntity entity = res.getEntity();
	if (entity != null) {
		result = EntityUtils.toString(entity);
	}
	logger.info("result:" + result);
} catch (Exception e) {
	logger.error("处理失败 {}" + e);
	e.printStackTrace();
} finally {
	if (response != null) {
		try {
			res.close();
		} catch (IOException e) {
			logger.error(e.getMessage());
		}
	}
}

 

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页