使用ChatGPT帮你提升代码编写能力

第一次ChatGPT代码沟通评审

使用ChatGPT帮你提升代码编写能力_第1张图片

import org.apache.http.*;
import org.apache.http.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.conn.ssl.*;
import org.apache.http.entity.*;
import org.apache.http.impl.client.*;
import org.apache.http.ssl.*;
import org.apache.http.util.*;
import org.apache.logging.log4j.*;

import java.io.*;

public class OpenPlatformHttpsUtilNew {
    private static final Logger logger = LogManager.getLogger(OpenPlatformHttpsUtilNew.class);

    public static String post(String url, String reqJson, String charsetName) {
        if (charsetName == null || charsetName.isBlank()) {
            charsetName = "UTF-8";
        }

        try {
            SSLConnectionSocketFactory sslsf = createSslSocketFactory();
            CloseableHttpClient httpClient = createHttpClient(url, sslsf);

            HttpPost httpPost = createHttpPost(url, charsetName, reqJson);

            try (CloseableHttpResponse httpResponse = httpClient.execute(httpPost)) {
                HttpEntity entity = httpResponse.getEntity();
                String body = EntityUtils.toString(entity, charsetName);
                logger.info("向" + url + "发起HTTP,JSON请求,返回值为:" + body);
                EntityUtils.consume(entity);
                return body;
            }
        } catch (IOException e) {
            logger.error("IOException", e);
        } catch (Exception e) {
            logger.error("Exception", e);
        }
        return null;
    }

    private static SSLConnectionSocketFactory createSslSocketFactory() {
        return new SSLConnectionSocketFactory(
                SSLContexts.createDefault(),
                new String[]{"TLSv1.2", "TLSv1.3"},
                null,
                SSLConnectionSocketFactory.getDefaultHostnameVerifier());
    }

    private static CloseableHttpClient createHttpClient(String url, SSLConnectionSocketFactory sslsf) {
        if (url != null && url.startsWith("https")) {
            return HttpClients.custom().setSSLSocketFactory(sslsf).build();
        } else {
            return HttpClientBuilder.create().build();
        }
    }

    private static HttpPost createHttpPost(String url, String charsetName, String reqJson) {
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Content-type", "application/json;charset=UTF-8");
        httpPost.setHeader("Accept-Type", charsetName);
        httpPost.setEntity(new StringEntity(reqJson, "UTF-8"));

        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(url.startsWith("https") ? 600000 : 300 * 1000)
                .setConnectTimeout(url.startsWith("https") ? 600000 : 300 * 1000)
                .build();
        httpPost.setConfig(requestConfig);

        return httpPost;
    }
}

在这里插入图片描述

第二次沟通代码评审

使用ChatGPT帮你提升代码编写能力_第2张图片

import org.apache.http.*;
import org.apache.http.client.methods.*;
import org.apache.http.conn.ssl.*;
import org.apache.http.entity.*;
import org.apache.http.impl.client.*;
import org.apache.http.ssl.*;
import org.apache.http.util.*;
import org.apache.logging.log4j.*;

import java.io.*;

public class OpenPlatformHttpsUtilNew {
    private static final Logger logger = LogManager.getLogger(OpenPlatformHttpsUtilNew.class);

    public static String post(String url, String reqJson, String charsetName) {
        if (charsetName == null || charsetName.isBlank()) {
            charsetName = "UTF-8";
        }

        try (CloseableHttpClient httpClient = createHttpClient(url);
             CloseableHttpResponse httpResponse = executeHttpPost(url, reqJson, charsetName, httpClient)) {

            HttpEntity entity = httpResponse.getEntity();
            String body = EntityUtils.toString(entity, charsetName);
            logger.info("向" + url + "发起HTTP,JSON请求,返回值为:" + body);
            EntityUtils.consume(entity);
            return body;
        } catch (IOException e) {
            logger.error("IOException", e);
        } catch (Exception e) {
            logger.error("Exception", e);
        }
        return null;
    }

    private static SSLConnectionSocketFactory createSslSocketFactory() {
        return new SSLConnectionSocketFactory(
                SSLContexts.createDefault(),
                new String[]{"TLSv1.2", "TLSv1.3"},
                null,
                SSLConnectionSocketFactory.getDefaultHostnameVerifier());
    }

    private static CloseableHttpClient createHttpClient(String url) {
        if (url != null && url.startsWith("https")) {
            SSLConnectionSocketFactory sslsf = createSslSocketFactory();
            return HttpClients.custom().setSSLSocketFactory(sslsf).build();
        } else {
            return HttpClientBuilder.create().build();
        }
    }

    private static CloseableHttpResponse executeHttpPost(String url, String reqJson, String charsetName, CloseableHttpClient httpClient) throws IOException {
        HttpPost httpPost = createHttpPost(url, charsetName, reqJson);
        return httpClient.execute(httpPost);
    }

    private static HttpPost createHttpPost(String url, String charsetName, String reqJson) {
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Content-type", "application/json;charset=UTF-8");
        httpPost.setHeader("Accept-Type", charsetName);
        httpPost.setEntity(new StringEntity(reqJson, "UTF-8"));

        int timeout = url.startsWith("https") ? 600000 : 300 * 1000;
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(timeout)
                .setConnectTimeout(timeout)
                .build();
        httpPost.setConfig(requestConfig);

        return httpPost;
    }
}

使用ChatGPT帮你提升代码编写能力_第3张图片
欢迎沟通交流学习

你可能感兴趣的:(java)