有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java HTTPS POST调用不适用于RestTemplate或Apache Httpclient 4。但与邮递员客户合作

当我用postman提供的必要标题调用一些httpsrest端点with POST方法时,它就工作得很好了

我试图用以下基于java的方法将同一个REST服务称为REST服务,并得到相同的错误(如下所示):

  1. 使用带Rest模板的Spring boot(1.5.3) 环境:Windows 7,java版本:1.8.0_192

代码片段:

String postJsonBody = getJsonBody();

HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type","application/json");
headers.add("Authorization", Token);

HttpEntity<String> requestEntity = new HttpEntity<>(postJsonBody, headers);
ResponseEntity<String> response = restTemplate.exchange("https://myservice.url", HttpMethod.POST, requestEntity, String.class);
  1. 使用版本为4.5.3的Apache HTTP客户端

代码片段:

CloseableHttpClient client = HttpClients
       .custom()
       .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
       .build();

HttpPost httpPost = new HttpPost(getURL());

String json = getJsonBody();
StringEntity entity = new StringEntity(json);
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("Authorization", Token);

CloseableHttpResponse response = client.execute(httpPost);
client.close();
response.close();

错误:

    16:28:17.902 [main] DEBUG org.apache.http.impl.conn.DefaultManagedHttpClientConnection - http-outgoing-0: Shutdown connection
16:28:17.902 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Connection discarded
16:28:17.902 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection released: [id: 0][route: ...][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
16:28:17.904 [main] INFO org.apache.http.impl.execchain.RetryExec - I/O exception (java.net.SocketException) caught when processing request to ... : Connection reset
16:28:17.907 [main] DEBUG org.apache.http.impl.execchain.RetryExec - Connection reset
java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at sun.security.ssl.InputRecord.readFully(Unknown Source)
    at sun.security.ssl.InputRecord.read(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.waitForClose(Unknown Source)
    at sun.security.ssl.HandshakeOutStream.flush(Unknown Source)
    at sun.security.ssl.Handshaker.kickstart(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.kickstartHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:396)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:355)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:359)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:381)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)

有人能提出根本原因和解决办法吗


共 (0) 个答案