1

I need to establish and send/read over/from an https connection (to a website of course) but through an http proxy or SOCKS proxy. A few other requirements

  • supports blocking (I can't use non-blocking/nio)
  • isn't set as an environment or some other global scope property (there are multiple threads accessing)

I was looking into HttpCore components but I did not see any support for blocking https.

4 Answers 4

3

Look at the java.net.Proxy class. That does what you need. You create one, and then pass it to the URLConnection to create the connection.

Sign up to request clarification or add additional context in comments.

2 Comments

Hm, is there a way I can tell if it connected via socks4 or socks5? I can only specify "socks" as a type and it seems to try to guess between socsk4,socks4a and socks5?
@Zombies, it seems that it attempts to figure it out itself, yes.
2

To support per-thread proxy, your best bet is Apache HttpClient 4 (Http Components Client). Get the source code,

http://hc.apache.org/downloads.cgi

It comes with examples for both HTTP proxy and SOCKS proxy,

   ClientExecuteProxy.java
   ClientExecuteSOCKS.java

3 Comments

Are you sure those examples are packaged in there? a google result shows almost nothing for that file. And a folder search found nothing.
Try get the source from the SVN. Or you can get it online from here: svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/…
1

Did you look at Apache HTTP Client? Haven't used it in ages but I did use it to pick a proxy server dynamically. Example from site here:

 HttpClient httpclient = new HttpClient();
  httpclient.getHostConfiguration().setProxy("myproxyhost", 8080);
  httpclient.getState().setProxyCredentials("my-proxy-realm", " myproxyhost",
  new UsernamePasswordCredentials("my-proxy-username", "my-proxy-password"));
  GetMethod httpget = new GetMethod("https://www.verisign.com/");
  try { 
    httpclient.executeMethod(httpget);
    System.out.println(httpget.getStatusLine());
  } finally {
    httpget.releaseConnection();
  }

3 Comments

Hm, but how can this be set to socks4/5/http? It seems only http proxy is accepted?
Socks is going to make that much harder. You're likly going to have to hack something up to use the SocketFactory class to pass that socket to the HTTPClient. This is really an ugly problem for a Friday afternoon.
@ZZ Coder mentioned the newer HttpClient4 and that might fix the socks issue.
0
System.setProperty("http.proxyHost", "proxy.com");
System.setPropery("http.proxyPort", "8080");

URL url = new URL("http://java.sun.com/");
InputStream in = url.openStream();

http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html

1 Comment

Sorry, but this won't work because it sets a property and I have multiple threads using different proxies

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.