Thursday, 8 August 2019

HttpClient's post request doesn't reach the outside world

The issue is HttpClient's post request cannot reach the outside world due to the Internet access restriction. Even after having configured the proxy on jvm, it still doesn't work.

The error message reads

14:31:06.604 java[31504]: 2019-08-06 14:31:06.604  INFO 31504 --- [tp2088371948-52] o.apache.http.impl.execchain.RetryExec   : Retrying request to {s}->https://www.google.com:443
14:33:13.837 java[31504]: 2019-08-06 14:33:13.837 ERROR 31504 --- [tp2088371948-52] a.g.n.t.service.utils.HttpClientUtil     : Post request failed: https://www.google.com/recaptcha/api/siteverify
14:33:13.837 java[31504]: 
14:33:13.837 java[31504]: java.net.SocketException: Network is unreachable (connect failed)
14:33:13.837 java[31504]:       at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_222]
14:33:13.837 java[31504]:       at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_222]
14:33:13.837 java[31504]:       at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_222]
14:33:13.837 java[31504]:       at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_222]
14:33:13.837 java[31504]:       at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_222]
14:33:13.837 java[31504]:       at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_222]
14:33:13.837 java[31504]:       at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:339) ~[httpclient-4.5.6.jar!/:4.5.6]

The solution is using HttpClients.createSystem() instead of HttpCients.createDefault() to create the HttpClient instance.

 createSystem

public static CloseableHttpClient createSystem()

    Creates CloseableHttpClient instance with default configuration based on system properties. 

No comments:

Post a Comment