Вызов HTTPS POST не работает с RestTemplate или Apache Httpclient 4.x, но работает с клиентом Postman - PullRequest
0 голосов
/ 04 марта 2019

Когда я вызываю некоторую https конечную точку отдыха с методом POST с необходимыми заголовками из почтальона, тогда он работает отлично.

Я пытаюсь вызвать тот же сервис REST со следующими подходами на основе Java и получить ту же ошибку (как указано ниже) для всех:

  1. Использование Spring boot (1.5.3)с шаблоном Rest Rest Environment: 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);
Использование HTTP-клиента Apache с версией 4.5.3

Фрагмент кода:

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)

Может кто-нибудь подсказать, что можетбыть основной причиной и исправить?

...