How to configure apache httpcore 4 to use a proxy server?

I am trying to use the latest (4.0.1) Apache http core components library. However, my web browser goes through a proxy - suppose this is myproxy.com:9191. Can someone provide some sample code to get simple http to use this as a proxy?

I tried adding the following at the beginning of my code, but had no joy:

ProxySelector.setDefault(new ProxySelector() { public List<Proxy> select(URI uri) { Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("myproxy.com", 9191); return Arrays.asList(new Proxy[]{proxy)}); } public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { ioe.printStackTrace(); } }); 
+4
source share
1 answer

In the absence of an answer, here is what I found out.

First of all, for this kind of thing, you do not just want to use the http core library, you want to use httpclient , make sure you load both pages from the download page.

Secondly, use this code:

 DefaultHttpClient httpclient = new DefaultHttpClient(); HttpHost proxy = new HttpHost("myproxy.com", 9191); httpclient.getCredentialsProvider().setCredentials( new AuthScope(PROXY, PROXY_PORT), new UsernamePasswordCredentials("username", "password")); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); 
+9
source

All Articles