kongzy
2024-07-01 47a751cb301d05276ae5d75145d57b2d090fe4e1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.nanometer.smartlab.util;
 
import com.nanometer.smartlab.exception.BusinessException;
import com.nanometer.smartlab.exception.ExceptionEnumCode;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
 
public class HttpUtil {
 
 
    public static String doGet(String url, List<NameValuePair> params){
        try{
            CloseableHttpClient client = HttpClients.createDefault();
            URIBuilder builder = new URIBuilder(url);
            URIBuilder uriBuilder = builder.setParameters(params);
 
            RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(120000)
                    .setSocketTimeout(120000).setConnectTimeout(120000).build();  //设置超时
            HttpGet get = new HttpGet(uriBuilder.build());
            get.setConfig(requestConfig);
            CloseableHttpResponse res = client.execute(get);
            return EntityUtils.toString(res.getEntity(), "UTF-8");
        } catch (IOException | URISyntaxException e) {
            e.printStackTrace();
            throw new BusinessException(ExceptionEnumCode.SYS_ERR, "GET请求发生错误,请检查代码");
        }
 
 
    }
}