Home:ALL Converter>Common HTTPclient and proxy

Common HTTPclient and proxy

Ask Time:2012-03-22T03:51:34         Author:dario111cro

Json Formatter

I am using apache's common httpclient library. Is it possible to make HTTP request over proxy? More specific, I need to use proxy list for multithreaded POST requests (right now I am testing with single threaded GET requests).

I tried to use:

        httpclient.getHostConfiguration().setProxy("67.177.104.230", 58720);

I get errors with that code:

21.03.2012. 20:49:17 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: I/O exception (java.net.ConnectException) caught when processing request: Connection refused: connect
21.03.2012. 20:49:17 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: Retrying request
21.03.2012. 20:49:19 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: I/O exception (java.net.ConnectException) caught when processing request: Connection refused: connect
21.03.2012. 20:49:19 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: Retrying request
21.03.2012. 20:49:21 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: I/O exception (java.net.ConnectException) caught when processing request: Connection refused: connect
21.03.2012. 20:49:21 org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: Retrying request
org.apache.commons.httpclient.ProtocolException: The server xxxxx failed to respond with a valid HTTP response
    at org.apache.commons.httpclient.HttpMethodBase.readStatusLine(HttpMethodBase.java:1846)
    at org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodBase.java:1590)
    at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:995)
    at org.apache.commons.httpclient.ConnectMethod.execute(ConnectMethod.java:144)
    at org.apache.commons.httpclient.HttpMethodDirector.executeConnect(HttpMethodDirector.java:495)
    at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:390)
    at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:170)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:396)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:324)
    at test.main(test.java:42)

When I remove that line, everything runs fine as expected.

Author:dario111cro,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/9811828/common-httpclient-and-proxy
Kai Sternad :

For httpclient 4.1.x you can set the proxy like this (taken from this example):\n\n HttpHost proxy = new HttpHost(\"127.0.0.1\", 8080, \"http\");\n\n DefaultHttpClient httpclient = new DefaultHttpClient();\n try {\n httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);\n\n HttpHost target = new HttpHost(\"issues.apache.org\", 443, \"https\");\n HttpGet req = new HttpGet(\"/\");\n\n System.out.println(\"executing request to \" + target + \" via \" + proxy);\n HttpResponse rsp = httpclient.execute(target, req);\n ...\n } finally {\n // When HttpClient instance is no longer needed,\n // shut down the connection manager to ensure\n // immediate deallocation of all system resources\n httpclient.getConnectionManager().shutdown();\n }\n",
2012-03-21T20:01:09
Michael Laffargue :

Here is how to do that with the last version of HTTPClient (4.3.4)\n\n CloseableHttpClient httpclient = HttpClients.createDefault();\n try {\n HttpHost target = new HttpHost(\"localhost\", 443, \"https\");\n HttpHost proxy = new HttpHost(\"127.0.0.1\", 8080, \"http\");\n\n RequestConfig config = RequestConfig.custom()\n .setProxy(proxy)\n .build();\n HttpGet request = new HttpGet(\"/\");\n request.setConfig(config);\n\n System.out.println(\"Executing request \" + request.getRequestLine() + \" to \" + target + \" via \" + proxy);\n\n CloseableHttpResponse response = httpclient.execute(target, request);\n try {\n System.out.println(\"----------------------------------------\");\n System.out.println(response.getStatusLine());\n EntityUtils.consume(response.getEntity());\n } finally {\n response.close();\n }\n } finally {\n httpclient.close();\n }\n",
2014-06-18T07:33:34
Dungeon Hunter :

Starting from Apache HTTPComponents 4.3.x HttpClientBuilder class sets the proxy defaults from System properties http.proxyHost and http.proxyPort or else you can override them using setProxy method.",
2013-11-15T08:50:30
Santosh Singh :

Although this question is very old, but I see still there are no exact answer. I will try to answer the question here. \n\nI believe the question in short here is how to set the proxy settings for the Apache commons HttpClient (org.apache.commons.httpclient.HttpClient). \n\nCode snippet below should work :\n\nHttpClient client = new HttpClient();\nHostConfiguration hostConfiguration = client.getHostConfiguration();\nhostConfiguration.setProxy(\"localhost\", 8080);\nclient.setHostConfiguration(hostConfiguration);\n",
2015-01-13T06:01:05
rakensi :

Here is how I solved this problem for the old (< 4.3) HttpClient (which I cannot upgrade), using the answer of Santosh Singh (who I gave a +1):\n\nHttpClient httpclient = new HttpClient();\nif (System.getProperty(\"http.proxyHost\") != null) {\n try {\n HostConfiguration hostConfiguration = httpclient.getHostConfiguration();\n hostConfiguration.setProxy(System.getProperty(\"http.proxyHost\"), Integer.parseInt(System.getProperty(\"http.proxyPort\")));\n httpclient.setHostConfiguration(hostConfiguration);\n this.getLogger().warn(\"USING PROXY: \"+httpclient.getHostConfiguration().getProxyHost());\n } catch (Exception e) {\n throw new ProcessingException(\"Cannot set proxy!\", e);\n }\n}\n",
2016-02-01T16:31:09
ttati :

I had a similar problem with HttpClient version 4.\n\nI couldn't connect to the server because of a SOCKS proxy error and I fixed it using the below configuration:\n\nclient.getParams().setParameter(\"socksProxyHost\",proxyHost);\nclient.getParams().setParameter(\"socksProxyPort\",proxyPort);\n",
2013-07-24T11:49:56
yy