HttpClient实现模拟浏览器请求返回响应信息
public class HttpUtil{
public static String doPost(String params, String url) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = null;
CloseableHttpResponse response = null;
String responseStr = null;
try {
httpPost = new HttpPost(url);
httpPost.addHeader("content-type", "application/json");
httpPost.addHeader("catch-control", "no-cache");
StringEntity entity = new StringEntity(params, "UTF-8");
httpPost.setEntity(entity);
LogUtil.info("----------------开始请求----------------");
response = httpClient.execute(httpPost);
LogUtil.info("----------------请求结束----------------");
responseStr = EntityUtils.toString(response.getEntity(), "UTF-8");
} catch (Exeception e) {
e.printStackTrace();
}
return responseStr;
}
}