title: Connecting with different Proxies to specific addresses
body: <p>I am developing a Java webservice application (with JAX-WS) that has to use two different proxies to establish separated connections to internet and an intranet. As solution I tried to write my own <em>java.net.ProxySelector</em> that returns a <em>java.net.Proxy</em> instance (of type HTTP) for internet or intranet.</p>
  
  <p>In a little test application I try to download webpage via <em>URL.openConnection()</em>, and before I replaced the default ProxySelector with my own. But it results in an exception:</p>
  
  <blockquote>
    <p>java.net.SocketException: Unknown proxy type : HTTP
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:370)
        at java.net.Socket.connect(Socket.java:519)
        at java.net.Socket.connect(Socket.java:469)
        at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
        at sun.net.www.http.HttpClient.(HttpClient.java:233)
        at sun.net.www.http.HttpClient.New(HttpClient.java:306)
        at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:844)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:792)
        at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:703)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1026)
        at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:373)
        at norman.test.ProxyTest.conntectToRmViaProxy(ProxyTest.java:42)
        at norman.test.ProxyTest.main(ProxyTest.java:65)</p>
  </blockquote>
  
  <ol>
  <li>Question: "Why tries the application to establish a connection via SOCKS, if my ProxySelector only returns a HTTP Proxy?"</li>
  </ol>
  
  <p>2 Question: "Is there a alternative, to define different proxies for each connection?"</p>
  
  <p>This is my ProxySelector:</p>
  
  <pre><code>public class OwnProxySelector extends ProxySelector {
  private Proxy intranetProxy;
  private Proxy extranetProxy;
  private Proxy directConnection = Proxy.NO_PROXY;
  private URI intranetAddress;
  private URI extranetAddress;
  
  /* (non-Javadoc)
   * @see java.net.ProxySelector#connectFailed(java.net.URI, java.net.SocketAddress, java.io.IOException)
   */
  public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
      // Nothing to do
  }
  
  /* (non-Javadoc)
   * @see java.net.ProxySelector#select(java.net.URI)
   */
  public List select(URI uri) {
      ArrayList&lt;Proxy&gt; result = new ArrayList&lt;Proxy&gt;();
  
      if(intranetAddress.getHost().equals(uri.getHost()) &amp;&amp; intranetAddress.getPort()==uri.getPort()){
          result.add(intranetProxy);
          System.out.println("Adding intranet Proxy!");
      }
      else if(extranetAddress.getHost().equals(uri.getHost()) &amp;&amp; extranetAddress.getPort()==uri.getPort()){
          result.add(extranetProxy);
          System.out.println("Adding extranet Proxy!");
      }
      else{
          result.add(directConnection);
          System.out.println("Adding direct connection!");
      }
  
      return result;
  }
  
  public void setIntranetProxy(String proxyAddress, int proxyPort){
      if(proxyAddress==null || proxyAddress.isEmpty()){
          intranetProxy = Proxy.NO_PROXY;
      }
      else{
          SocketAddress address = new InetSocketAddress(proxyAddress, proxyPort);
          intranetProxy = new Proxy(Proxy.Type.HTTP, address);
      }
  }
  
  public void setExtranetProxy(String proxyAddress, int proxyPort){
      if(proxyAddress==null || proxyAddress.isEmpty()){
          extranetProxy = Proxy.NO_PROXY;
      }
      else{
          SocketAddress address = new InetSocketAddress(proxyAddress, proxyPort);
          extranetProxy = new Proxy(Proxy.Type.HTTP, address);
      }
  }
  
  public void clearIntranetProxy(){
      intranetProxy = Proxy.NO_PROXY;
  }
  
  public void clearExtranetProxy(){
      extranetProxy = Proxy.NO_PROXY;
  }
  
  public void setIntranetAddress(String address) throws URISyntaxException{
      intranetAddress = new URI(address);
  }
  
  public void setExtranetAddress(String address) throws URISyntaxException{
      extranetAddress = new URI(address);
  }
  }
  </code></pre>
  
  <p>This is the test class:</p>
  
  <pre><code>public class ProxyTest {
  OwnProxySelector ownSelector = new OwnProxySelector();
  
  public ProxyTest(){
      ownSelector.setIntranetProxy("intranet.proxy", 8123);
      try {
          ownSelector.setIntranetAddress("http://intranet:80");
      } catch (URISyntaxException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
      ownSelector.setExtranetProxy("", 0);
      try {
          ownSelector.setExtranetAddress("http://www.example.com:80");
      } catch (URISyntaxException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
  
      ProxySelector.setDefault(ownSelector);
  }
  
  public void conntectToRmViaProxy(boolean internal, String connectAddress){
      try {
          URL url = new URL(connectAddress);
  
          HttpURLConnection conn = (HttpURLConnection)url.openConnection();
  
          conn.setRequestMethod("GET");
            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
              System.out.println(conn.getResponseMessage());
            }
            else{
                BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                int tmp = reader.read();
                while(tmp != -1){
                    System.out.print((char)tmp);
                    tmp = reader.read();
                }
            }
  
      } catch (MalformedURLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  
  public static void main(String[] args){
      ProxyTest proxyText = new ProxyTest();
      proxyText.conntectToRmViaProxy(true, "http://intranet:80");
  }
  }
  </code></pre>