packagecom.ray.dingtalk.qy.util;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importorg.apache.http.HttpEntity;importorg.apache.http.HttpStatus;importorg.apache.http.client.config.RequestConfig;importorg.apache.http.client.methods.CloseableHttpResponse;importorg.apache.http.client.methods.HttpGet;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.entity.ContentType;importorg.apache.http.entity.StringEntity;importorg.apache.http.entity.mime.MultipartEntityBuilder;importorg.apache.http.entity.mime.content.FileBody;importorg.apache.http.impl.client.CloseableHttpClient;importorg.apache.http.impl.client.HttpClients;importorg.apache.http.protocol.BasicHttpContext;importorg.apache.http.util.EntityUtils;importorg.apache.logging.log4j.LogManager;importorg.apache.logging.log4j.Logger;importcom.alibaba.fastjson.JSON;importcom.alibaba.fastjson.JSONObject;/*** HTTP请求封装,建议直接使用sdk的API*/
public classHttpHelper {private static final Logger log = LogManager.getLogger(HttpHelper.class);/*** @desc :1.发起GET请求
*
*@paramurl
*@returnJSONObject
*@throwsException*/
public static JSONObject doGet(String url) throwsException {//1.生成一个请求
HttpGet httpGet = newHttpGet(url);//2.配置请求的属性
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build();//2000
httpGet.setConfig(requestConfig);//3.发起请求,获取响应信息//3.1 创建httpClient
CloseableHttpClient httpClient =HttpClients.createDefault();
CloseableHttpResponse response= null;try{//3.2 发起请求,获取响应信息
response = httpClient.execute(httpGet, newBasicHttpContext());//如果返回结果的code不等于200,说明出错了
if (response.getStatusLine().getStatusCode() != 200) {
log.info("request url failed, http code=" +response.getStatusLine().getStatusCode()+ ", url="