{"id":6216,"date":"2014-04-13T23:30:09","date_gmt":"2014-04-13T23:30:09","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/13\/with-httpclient-4-3-x-executing-a-httphead-for-a-specific-url-gives-nohttpresponseexception-collection-of-common-programming-errors\/"},"modified":"2014-04-13T23:30:09","modified_gmt":"2014-04-13T23:30:09","slug":"with-httpclient-4-3-x-executing-a-httphead-for-a-specific-url-gives-nohttpresponseexception-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/13\/with-httpclient-4-3-x-executing-a-httphead-for-a-specific-url-gives-nohttpresponseexception-collection-of-common-programming-errors\/","title":{"rendered":"With HttpClient 4.3.x, executing a HttpHead for a specific URL gives NoHttpResponseException-Collection of common programming errors"},"content":{"rendered":"<p>Using the test class below, the line at <code>\/\/\/\/\/ 1 \/\/\/\/\/<\/code> throws a <code>NoHttpResponseException<\/code> (see exception trace with logging at the end of the question). Executing any of the other lines <code>\/\/\/\/\/ 2-to-8 \/\/\/\/\/<\/code> prints out the headers fine. Is this a bug in HttpClient 4.3.x or am I doing something wrong (I have tested this with both 4.3.1 and 4.3.2)?<\/p>\n<pre><code>import java.util.Arrays;\n\nimport org.apache.http.client.methods.*;\nimport org.apache.http.impl.client.*;\nimport org.apache.http.params.*;\n\npublic class PrintHeaders {\n  private static final String MONEY_SMART_URL = \"https:\/\/www.moneysmart.gov.au\/?\";\n  private static final String TGA_URL = \"https:\/\/www.ebs.tga.gov.au\/ebs\/picmi\/picmirepository.nsf\/PICMI?OpenForm&amp;t=&amp;k=P\";\n  private static final String DEFAULT_USER_AGENT = \"Mozilla\/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko\/20100101 Firefox\/26.0\";\n\n  public static void main(String[] args) throws Exception {\n    printHeadersByHead_4_3(TGA_URL);  \/\/\/\/\/ 1 \/\/\/\/\/\n\/\/    printHeadersByHead_4_2(TGA_URL);  \/\/\/\/\/ 2 \/\/\/\/\/\n\/\/    printHeadersByGet_4_3(TGA_URL);   \/\/\/\/\/ 3 \/\/\/\/\/\n\/\/    printHeadersByGet_4_2(TGA_URL);   \/\/\/\/\/ 4 \/\/\/\/\/\n\/\/    printHeadersByHead_4_3(MONEY_SMART_URL);  \/\/\/\/\/ 5 \/\/\/\/\/\n\/\/    printHeadersByHead_4_2(MONEY_SMART_URL);  \/\/\/\/\/ 6 \/\/\/\/\/\n\/\/    printHeadersByGet_4_3(MONEY_SMART_URL);   \/\/\/\/\/ 7 \/\/\/\/\/\n\/\/    printHeadersByGet_4_2(MONEY_SMART_URL);   \/\/\/\/\/ 8 \/\/\/\/\/\n  }\n\n  public static void printHeadersByHead_4_3(String docURL) {\n    printHeaders(new HttpHead(docURL), buildHttpClient_4_3());\n  }\n\n  public static void printHeadersByHead_4_2(String docURL) {\n    printHeaders(new HttpHead(docURL), buildHttpClient_4_2());\n  }\n\n  public static void printHeadersByGet_4_3(String docURL) {\n    printHeaders(new HttpGet(docURL), buildHttpClient_4_3());\n  }\n\n  public static void printHeadersByGet_4_2(String docURL) {\n    printHeaders(new HttpGet(docURL), buildHttpClient_4_2());\n  }\n\n  public static void printHeaders(HttpRequestBase req, CloseableHttpClient client) {\n    CloseableHttpResponse response = null;\n    try {\n      try {\n        response =  client.execute(req);\n        System.out.println(Arrays.asList(response.getAllHeaders()));\n      } finally {\n        if (response != null) response.close();\n        if (client != null) client.close();\n      }    \n    } catch (Exception e) {\n      e.printStackTrace();\n    }\n  }\n\n  public static CloseableHttpClient buildHttpClient_4_3() {\n    return HttpClients.custom().setUserAgent(DEFAULT_USER_AGENT).build();\n  }\n\n  @SuppressWarnings(\"deprecation\")\n  public static CloseableHttpClient buildHttpClient_4_2() {\n    AbstractHttpClient httpClient = new DefaultHttpClient();\n    httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, DEFAULT_USER_AGENT);\n    return httpClient;\n  }\n}\n<\/code><\/pre>\n<p>EXCEPTION TRACE when running line at <code>\/\/\/\/\/ 1 \/\/\/\/\/<\/code>:<\/p>\n<pre><code>2014\/02\/17 09:36:43:857 EST [DEBUG] RequestAddCookies - CookieSpec selected: best-match\n2014\/02\/17 09:36:43:871 EST [DEBUG] RequestAuthCache - Auth cache not set in the context\n2014\/02\/17 09:36:43:873 EST [DEBUG] PoolingHttpClientConnectionManager - Connection request: [route: {s}-&gt;https:\/\/www.ebs.tga.gov.au:443][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]\n2014\/02\/17 09:36:43:890 EST [DEBUG] PoolingHttpClientConnectionManager - Connection leased: [id: 0][route: {s}-&gt;https:\/\/www.ebs.tga.gov.au:443][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]\n2014\/02\/17 09:36:43:902 EST [DEBUG] MainClientExec - Opening connection {s}-&gt;https:\/\/www.ebs.tga.gov.au:443\n2014\/02\/17 09:36:44:046 EST [DEBUG] HttpClientConnectionManager - Connecting to www.ebs.tga.gov.au\/161.146.233.4:443\n2014\/02\/17 09:36:45:038 EST [DEBUG] MainClientExec - Executing request HEAD \/ebs\/picmi\/picmirepository.nsf\/PICMI?OpenForm&amp;t=&amp;k=P HTTP\/1.1\n2014\/02\/17 09:36:45:038 EST [DEBUG] MainClientExec - Target auth state: UNCHALLENGED\n2014\/02\/17 09:36:45:039 EST [DEBUG] MainClientExec - Proxy auth state: UNCHALLENGED\n2014\/02\/17 09:36:45:041 EST [DEBUG] headers - http-outgoing-0 &gt;&gt; HEAD \/ebs\/picmi\/picmirepository.nsf\/PICMI?OpenForm&amp;t=&amp;k=P HTTP\/1.1\n2014\/02\/17 09:36:45:041 EST [DEBUG] headers - http-outgoing-0 &gt;&gt; Host: www.ebs.tga.gov.au\n2014\/02\/17 09:36:45:041 EST [DEBUG] headers - http-outgoing-0 &gt;&gt; Connection: Keep-Alive\n2014\/02\/17 09:36:45:041 EST [DEBUG] headers - http-outgoing-0 &gt;&gt; User-Agent: Mozilla\/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko\/20100101 Firefox\/26.0\n2014\/02\/17 09:36:45:042 EST [DEBUG] headers - http-outgoing-0 &gt;&gt; Accept-Encoding: gzip,deflate\n2014\/02\/17 09:37:00:222 EST [DEBUG] DefaultManagedHttpClientConnection - http-outgoing-0: Close connection\n2014\/02\/17 09:37:00:222 EST [DEBUG] DefaultManagedHttpClientConnection - http-outgoing-0: Shutdown connection\n2014\/02\/17 09:37:00:222 EST [DEBUG] MainClientExec - Connection discarded\n2014\/02\/17 09:37:00:222 EST [DEBUG] DefaultManagedHttpClientConnection - http-outgoing-0: Close connection\n2014\/02\/17 09:37:00:223 EST [DEBUG] PoolingHttpClientConnectionManager - Connection released: [id: 0][route: {s}-&gt;https:\/\/www.ebs.tga.gov.au:443][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]\n2014\/02\/17 09:37:00:224 EST [INFO] RetryExec - I\/O exception (org.apache.http.NoHttpResponseException) caught when processing request: The target server failed to respond\n2014\/02\/17 09:37:00:224 EST [DEBUG] RetryExec - The target server failed to respond org.apache.http.NoHttpResponseException: The target server failed to respond\n    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:143)\n    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)\n    at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:260)\n    at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:161)\n    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)\n    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\n    at java.lang.reflect.Method.invoke(Method.java:606)\n    at org.apache.http.impl.conn.CPoolProxy.invoke(CPoolProxy.java:138)\n    at com.sun.proxy.$Proxy0.receiveResponseHeader(Unknown Source)\n    at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:271)\n    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:123)\n    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:253)\n    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:194)\n    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:85)\n    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)\n    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)\n    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)\n    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)\n    at PerformHttpHead.printHeaders(PrintHeaders.java:43)\n    at PerformHttpHead.printHeadersByHead_4_3(PrintHeaders.java:24)\n    at PerformHttpHead.main(PrintHeaders.java:13)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Using the test class below, the line at \/\/\/\/\/ 1 \/\/\/\/\/ throws a NoHttpResponseException (see exception trace with logging at the end of the question). Executing any of the other lines \/\/\/\/\/ 2-to-8 \/\/\/\/\/ prints out the headers fine. Is this a bug in HttpClient 4.3.x or am I doing something wrong (I have tested [&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-6216","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6216","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=6216"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6216\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}