{"id":6217,"date":"2014-04-13T23:30:14","date_gmt":"2014-04-13T23:30:14","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/13\/exception-reusing-httpclient-https-connection-collection-of-common-programming-errors\/"},"modified":"2014-04-13T23:30:14","modified_gmt":"2014-04-13T23:30:14","slug":"exception-reusing-httpclient-https-connection-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/13\/exception-reusing-httpclient-https-connection-collection-of-common-programming-errors\/","title":{"rendered":"Exception reusing HttpClient https connection-Collection of common programming errors"},"content":{"rendered":"<p>I am using apache HttpClient. While debugging something, I found I am getting error when I execute the same HTTPS HEAD request for the second time using the same connection (connection kept alive). The host is https:\/\/dongshengcn.iriscouch.com. I am not sure where is wrong, is the server? is it a HttpClient bug? I tend to think this is a HttpClient bug somewhere. Anyways, I am wondering if someone can point me to the right direction. Here is the code and logging info:<\/p>\n<pre><code>import org.apache.http.HttpHost;\nimport org.apache.http.client.methods.HttpHead;\nimport org.apache.http.client.params.ClientPNames;\nimport org.apache.http.impl.client.DefaultHttpClient;\nimport org.apache.http.params.BasicHttpParams;\nimport org.apache.http.params.HttpConnectionParams;\nimport org.apache.http.params.HttpParams;\nimport org.apache.http.params.HttpProtocolParams;\nimport org.junit.Test;\n\nimport java.io.IOException;\n\n\/**\n * Created with IntelliJ IDEA.\n * User: dongsheng\n * Date: 1\/10\/13\n * Time: 4:10 PM\n * To change this template use File | Settings | File Templates.\n *\/\npublic class HttpClientTest {\n\n    private int connectionTimeout = 1000;\n    private int socketTimeout = 10000;\n    private String host = \"dongshengcn.iriscouch.com\";\n\/\/    private String host = \"dongshengcn.cloudant.com\";\n    private int port = 443;\n    boolean useExpectContinue = true;\n\n    public DefaultHttpClient configureClient() {\n        HttpParams params = new BasicHttpParams();\n        HttpProtocolParams.setUseExpectContinue(params, useExpectContinue);\n        HttpConnectionParams\n                .setConnectionTimeout(params, connectionTimeout);\n        HttpConnectionParams.setSoTimeout(params, socketTimeout);\n        HttpConnectionParams.setTcpNoDelay(params, Boolean.TRUE);\n\n        String protocol = \"https\";\n        params.setParameter(ClientPNames.DEFAULT_HOST, new HttpHost(host,\n                port, protocol));\n        DefaultHttpClient client = new DefaultHttpClient(params);\n\n        return client;\n    }\n\n    @Test\n    public void test_dafault_http_client() throws IOException {\n        String uri = \"\/ektorp-test\/\";\n        DefaultHttpClient client = configureClient();\n\n        org.apache.http.HttpResponse rsp = client.execute(new HttpHead(uri));\n        System.out.println(rsp.toString());\n\n        try {\n            org.apache.http.HttpResponse rsp2 = client.execute(new HttpHead(uri));\n            System.out.println(rsp.toString());\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n}\n\n\n\nRunning HttpClientTest\n2013\/01\/11 10:12:05:288 EST [DEBUG] BasicClientConnectionManager - Get connection for route {s}-&gt;https:\/\/dongshengcn.iriscouch.com:443\n2013\/01\/11 10:12:06:065 EST [DEBUG] DefaultClientConnectionOperator - Connecting to dongshengcn.iriscouch.com:443\n2013\/01\/11 10:12:07:201 EST [DEBUG] RequestAddCookies - CookieSpec selected: best-match\n2013\/01\/11 10:12:07:223 EST [DEBUG] RequestAuthCache - Auth cache not set in the context\n2013\/01\/11 10:12:07:223 EST [DEBUG] RequestTargetAuthentication - Target auth state: UNCHALLENGED\n2013\/01\/11 10:12:07:224 EST [DEBUG] RequestProxyAuthentication - Proxy auth state: UNCHALLENGED\n2013\/01\/11 10:12:07:224 EST [DEBUG] DefaultHttpClient - Attempt 1 to execute request\n2013\/01\/11 10:12:07:224 EST [DEBUG] DefaultClientConnection - Sending request: HEAD \/ektorp-test\/ HTTP\/1.1\n2013\/01\/11 10:12:07:226 EST [DEBUG] headers - &gt;&gt; HEAD \/ektorp-test\/ HTTP\/1.1\n2013\/01\/11 10:12:07:226 EST [DEBUG] headers - &gt;&gt; Host: dongshengcn.iriscouch.com:443\n2013\/01\/11 10:12:07:226 EST [DEBUG] headers - &gt;&gt; Connection: Keep-Alive\n2013\/01\/11 10:12:07:303 EST [DEBUG] DefaultClientConnection - Receiving response: HTTP\/1.1 404 Host not found\n2013\/01\/11 10:12:07:304 EST [DEBUG] headers - &gt; HEAD \/ektorp-test\/ HTTP\/1.1\n2013\/01\/11 10:12:07:311 EST [DEBUG] headers - &gt;&gt; Host: dongshengcn.iriscouch.com:443\n2013\/01\/11 10:12:07:311 EST [DEBUG] headers - &gt;&gt; Connection: Keep-Alive\n2013\/01\/11 10:12:07:314 EST [DEBUG] DefaultHttpResponseParser - Garbage in response: Host not found\n2013\/01\/11 10:12:07:314 EST [DEBUG] DefaultClientConnection - Connection 0.0.0.0:62168173.192.57.98:443 closed\n2013\/01\/11 10:12:07:315 EST [DEBUG] DefaultClientConnection - Connection 0.0.0.0:62168173.192.57.98:443 shut down\n2013\/01\/11 10:12:07:315 EST [DEBUG] BasicClientConnectionManager - Releasing connection org.apache.http.impl.conn.ManagedClientConnectionImpl@67c7980c\n2013\/01\/11 10:12:07:315 EST [DEBUG] BasicClientConnectionManager - Connection can be kept alive for 9223372036854775807 MILLISECONDS\norg.apache.http.client.ClientProtocolException\n    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:909)\n    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)\n    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)\n    at HttpClientTest.test_dafault_http_client(HttpClientTest.java:69)\n    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)\n    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)\n    at java.lang.reflect.Method.invoke(Method.java:597)\n    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)\n    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)\n    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)\n    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)\n    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)\n    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)\n    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)\n    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)\n    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)\n    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)\n    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)\n    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)\n    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)\n    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)\n    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)\n    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)\n    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)\n    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)\n    at java.lang.reflect.Method.invoke(Method.java:597)\n    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)\n    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)\n    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)\n    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)\n    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)\nCaused by: org.apache.http.ProtocolException: The server failed to respond with a valid HTTP response\n    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:103)\n    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:62)\n    at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:254)\n    at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:289)\n    at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:252)\n    at org.apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseHeader(ManagedClientConnectionImpl.java:191)\n    at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:300)\n    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:127)\n    at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:712)\n    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:517)\n    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)\n    ... 32 more\nTests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.411 sec\n<\/code><\/pre>\n<p>If I change the host to a difference one like dongshengcn.cloudant.com:443, and it works as expected.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am using apache HttpClient. While debugging something, I found I am getting error when I execute the same HTTPS HEAD request for the second time using the same connection (connection kept alive). The host is https:\/\/dongshengcn.iriscouch.com. I am not sure where is wrong, is the server? is it a HttpClient bug? I tend to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6217","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6217","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6217"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6217\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}