title: Each thread using its own proxy
body: <p>I'm trying to set up a Java program, where each thread can use its own proxy.</p>
  
  <p>Right now I only found a way to set a proxy globally. (<a href="http://docs.oracle.com/javase/6/docs/technotes/guides/net/proxies.html" rel="nofollow">http://docs.oracle.com/javase/6/docs/technotes/guides/net/proxies.html</a>)</p>
  
  <blockquote>
    <p>As mentioned earlier, these settings affect all http connections
    during the entire lifetime of the VM invoked with these options.
    However it is possible, using the System.setProperty() method, to have
    a slightly more dynamic behavior.</p>
    
    <p>Here is a code excerpt showing how this can be done:</p>
    
    <p>//Set the http proxy to webcache.mydomain.com:8080</p>
    
    <p>System.setProperty("http.proxyHost", "webcache.mydomain.com");
    System.setPropery("http.proxyPort", "8080");</p>
  </blockquote>
  
  <h1>Update</h1>
  
  <p>I tried using the proxy class, yet can't create a direct connection for when I don't want to use said proxy:</p>
  
  <pre><code>private void setProxy()
  {
      if(proxyUrl != null)
      {
          SocketAddress addr = new InetSocketAddress(proxyUrl, proxyPort);
          proxy = new Proxy(Proxy.Type.HTTP, addr);
      }
      else
      {
          proxy = new Proxy(Proxy.Type.DIRECT, null);
      }       
  }
  </code></pre>
  
  <p><code>Exception in .... java.lang.IllegalArgumentException: type DIRECT is not compatible with address null</code></p>
  
  <p>How can I get this to work for direct connections? Haven't tried proxies yet.</p>