【问题标题】:Unable to send post https request showing SocketException: Connection reset无法发送显示 SocketException 的 post https 请求:连接重置
【发布时间】:2024-01-19 16:38:01
【问题描述】:

我正在发送 https 请求。我已添加客户端证书并导入 jvm 仍然无法执行请求,显示连接请求。

private static Registry<ConnectionSocketFactory> scheme = null;

static {

    SSLConnectionSocketFactory socketFactory = null;

    try {
        System.setProperty("javax.net.debug", "all");

        String KEYSTORE_LOCATION = "C:\\Users\\pritesha\\Desktop\\TS1_certs\\wwwin-ts1fin.cisco.com.jks";   
        FileInputStream is = new FileInputStream(new File(KEYSTORE_LOCATION));
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(is, "changeit".toCharArray());

        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(trustStore);
        SSLContext ctx = SSLContext.getInstance("TLSv1");
        ctx.init(null, tmf.getTrustManagers(), null);
        SSLSocketFactory sslFactory = ctx.getSocketFactory();


        SSLContextBuilder sslContextBuilder = SSLContexts.custom().useTLS().loadTrustMaterial(trustStore);
        socketFactory = new SSLConnectionSocketFactory(sslContextBuilder.build());
    } catch (KeyStoreException e) {
        System.out.println("Exception in the loading Key Store...  " + e.getMessage());
    } catch (KeyManagementException e) {
        System.out.println("Exception in the loading Key Store...  " + e.getMessage());
    } catch (NoSuchAlgorithmException e) {
        System.out.println("Exception in the loading Key Store...  " + e.getMessage());
    } catch (CertificateException e) {
        System.out.println("Exception in the loading Key Store...  " + e.getMessage());
    } catch (IOException e) {
        System.out.println("Exception in the loading Key Store...  " + e.getMessage());
    }
    scheme = RegistryBuilder.<ConnectionSocketFactory>create().register("https", socketFactory).build();
}

public static String connect() {
    CloseableHttpClient httpclient = null;
    try {
        String URL = "https://wwwin-ts1fin.cisco.com/bpelpcicreatereceipt_client_ep";
        String requestBody = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://xmlns.oracle.com/I2C/PrjPCIReceiptCreation/BPELPCICreateReceipt\"><SOAP-ENV:Header/><SOAP-ENV:Body>"
                + "<ns1:process><ns1:SourceSystem>CCW</ns1:SourceSystem> <ns1:PaymentId>" + 123
                + "</ns1:PaymentId> <ns1:Retry>No</ns1:Retry></ns1:process></SOAP-ENV:Body></SOAP-ENV:Envelope>";

        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(scheme);
        connectionManager.setMaxTotal(200);
        connectionManager.setDefaultMaxPerRoute(20);

        httpclient = HttpClients.custom().setConnectionManager(connectionManager).build();

        HttpPost httpPost = new HttpPost(URL);

        httpPost.setHeader("Content-Type", "text/xml");

        httpPost.setEntity(new StringEntity(requestBody));

        HttpResponse httpResponse = httpclient.execute(httpPost);

        HttpEntity entity = httpResponse.getEntity();

        if (entity != null) {
            return EntityUtils.toString(entity);
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

这是我得到的完整堆栈跟踪:

[原始读取]:长度 = 5 0000: 15 03 03 00 02 ..... 主要,处理异常:java.net.SocketException:连接重置 主要,发送 TLSv1 警报:致命,描述 = 意外消息主要,写入:TLSv1 警报,长度 = 2 主要,异常发送警报:java.net.SocketException:对等方重置连接:套接字写入错误 main,调用closeSocket()

【问题讨论】:

    标签: java certificate httpclient


    【解决方案1】:

    首先,这是一个 TLS 警报,您需要更好地了解它是什么警报。安装wireshark并嗅探会话。深入了解警报消息,wireshark 剖析器将解释更多问题。

    我怀疑您没有协商通用密码套件。您选择了 tls v1,但 cicso 可能有更高版本,因此您选择不通过降级到 tls 1.0(这是 ssl v3,被 cisco 拒绝)来提供一组密码套件。

    【讨论】: