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

第一次ChatGPT代码沟通评审

在这里插入图片描述

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;
    }
}

在这里插入图片描述

第二次沟通代码评审

在这里插入图片描述

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;
    }
}

在这里插入图片描述
欢迎沟通交流学习

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值