Chanseok Oh created HTTPCLIENT-2013:
---------------------------------------
Summary: Connect timeout throws HttpHostConnectException
(ConnectException) instead of ConnectTimeoutException (IOException)
Key: HTTPCLIENT-2013
URL: https://issues.apache.org/jira/browse/HTTPCLIENT-2013
Project: HttpComponents HttpClient
Issue Type: Bug
Components: HttpClient (classic)
Affects Versions: 4.5.9
Environment: Linux
openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build 1.8.0_222-8u222-b10-1-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)
Reporter: Chanseok Oh
It is obvious from the code in DefaultHttpClientConnectionOperator that it is
supposed to throw ConnectTimeoutException in the case of connection timeout:
{code:java}
} catch (final ConnectException ex) {
if (last) {
final String msg = ex.getMessage();
throw "Connection timed out".equals(msg)
? new ConnectTimeoutException(ex, host, addresses)
: new HttpHostConnectException(ex, host, addresses);
}
{code}
Recently, we've upgraded Apache HttpClient (indirectly through Google HTTP
Client), and our production code handling ConnectionException got broken due to
DefaultHttpClientConnectionOperator throwing HttpHostConnectException that
extends ConnectionException. (OTOH, ConnectTimeoutException is an IOException
and not a ConnectionException.)
Java version:
{code}
openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build 1.8.0_222-8u222-b10-1-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)
{code}
Example code to reproduce:
{code:java}
public static void main(String[] args) throws IOException {
// example.com is reserved by the DNS standard and will always trigger
timeout
try (CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse response = client.execute(new
HttpGet("https://example.com:81"))) {}
}
{code}
However, it currently throws the other exception HttpHostConnectException
rather than ConnectTimeoutException.
{code:java}
Exception in thread "main" org.apache.http.conn.HttpHostConnectException:
Connect to example.com:81 [example.com/93.184.216.34,
example.com/2606:2800:220:1:248:1893:25c8:1946] failed: Connection timed out
(Connection timed out)Exception in thread "main"
org.apache.http.conn.HttpHostConnectException: Connect to example.com:81
[example.com/93.184.216.34, example.com/2606:2800:220:1:248:1893:25c8:1946]
failed: Connection timed out (Connection timed out) at
org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:156)
at
org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:374)
at
org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:393)
at
org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89) at
org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) 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)
at com.example.ApacheHttpClient.main(ApacheHttpClient.java:13)Caused by:
java.net.ConnectException: Connection timed out (Connection timed out) at
java.net.PlainSocketImpl.socketConnect(Native Method) at
java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) at
java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at
java.net.Socket.connect(Socket.java:589) at
org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:368)
at
org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
... 10 more
{code}
--
This message was sent by Atlassian Jira
(v8.3.2#803003)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]