1
zj
2025-08-18 dba8004e599c935e572c20682b5d463f904e72aa
src/main/java/com/nq/utils/http/HttpClientRequest.java
@@ -17,6 +17,8 @@
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.platform.commons.logging.Logger;
import org.junit.platform.commons.logging.LoggerFactory;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
@@ -25,55 +27,46 @@
@Slf4j
public class HttpClientRequest {
    // 可配置的超时参数(单位:毫秒)
    private static final int CONNECT_TIMEOUT = 35000;
    private static final int CONNECTION_REQUEST_TIMEOUT = 35000;
    private static final int SOCKET_TIMEOUT = 60000;
    public static String doGet(String url) {
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        String result = "";
        // 1. 参数校验
        if (url == null || url.trim().isEmpty()) {
            throw new IllegalArgumentException("URL cannot be null or empty");
        }
        try {
            httpClient = HttpClients.createDefault();
        // 2. 创建HTTP客户端和请求对象
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpGet httpGet = new HttpGet(url);
            httpGet.setHeader("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
            httpGet.setHeader("Referer","https://quotes.sina.cn/hs/company/quotes/view/sz399001?vt=4&cid=76524&node_id=76524&autocallup=no&isfromsina=yes");
            //cookie
            httpGet.setHeader("Cookie", PropertiesUtil.getProperty("cookle"));
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000).setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();
            // 3. 配置请求参数
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(CONNECT_TIMEOUT)
                    .setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT)
                    .setSocketTimeout(SOCKET_TIMEOUT)
                    .build();
            httpGet.setConfig(requestConfig);
            response = httpClient.execute(httpGet);
            // 4. 执行请求并处理响应
            try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode != 200) {
                    throw new IOException("HTTP request failed with status code: " + statusCode);
                }
            HttpEntity entity = response.getEntity();
                HttpEntity entity = response.getEntity();
                if (entity == null) {
                    throw new IOException("Response entity is null");
                }
            result = EntityUtils.toString(entity);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
                return EntityUtils.toString(entity);
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("HTTP request failed", e);
        }
        finally {
            if (null != response) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != httpClient) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
    public static String doPost(String url, Map<String, Object> paramMap) {