title: Malformed Reply from SOCKS Server when fetching proxy server details dynamically
body: <p>I am trying to access a website through proxy server.I am using httpclient.</p>
  
  <p><strong>This is the code which is working fine:</strong></p>
  
  <pre><code>import org.apache.commons.httpclient.Credentials;
  import org.apache.commons.httpclient.HostConfiguration;
  import org.apache.commons.httpclient.HttpClient;
  import org.apache.commons.httpclient.HttpMethod;
  import org.apache.commons.httpclient.HttpStatus;
  import org.apache.commons.httpclient.UsernamePasswordCredentials;
  import org.apache.commons.httpclient.auth.AuthScope;
  import org.apache.commons.httpclient.methods.GetMethod;
  import java.io.IOException;
  import java.net.InetSocketAddress;
  import java.net.Proxy;
  import java.net.ProxySelector;
  import java.net.URI;
  import java.net.URISyntaxException;
  import java.util.Properties;
  
  public class Working {
  private static  String PROXY_HOST = "proxy.test.org";
  private static  int PROXY_PORT = 80;
  
      public static void main(String[] args) {
      HttpClient client = new HttpClient();
      HttpMethod method = new GetMethod("https://www.example.org");
      HostConfiguration config = client.getHostConfiguration();
      config.setProxy(PROXY_HOST, PROXY_PORT);
  
      try {
            client.executeMethod(method);
              if (method.getStatusCode() == HttpStatus.SC_OK) {
             String response = method.getResponseBodyAsString();
             System.out.println("Response = " + response);
                          }
      } catch (IOException e) {
          e.printStackTrace();
      } finally {
          method.releaseConnection();
      }
      System.out.println("end");
      }
  }
  </code></pre>
  
  <p><strong>But When I use following method to set PROXY_HOST and PROXY_PORT values dynamically I am getting "Malformed reply from SOCKS server" exception</strong></p>
  
  <pre><code>public static void getProxyDetails()
  {
      Properties systemSettings = System.getProperties();
      systemSettings.put("proxySet", "true");
      try {
          Proxy proxy = (Proxy) ProxySelector.getDefault().select(new URI(
                  "https://www.example2.com/xyz")).iterator().
               next();
           System.out.println("proxy hostname : " + proxy.type());
            InetSocketAddress addr = (InetSocketAddress)proxy.address();
            if (addr == null) {
                System.out.println("No Proxy");
             } else {
                System.out.println("proxy hostname : " + addr.getHostName());
                System.out.println("proxy port : " + addr.getPort());
                 PROXY_HOST = addr.getHostName();
                 PROXY_PORT = addr.getPort();            
             }
      } catch (URISyntaxException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>I tried other suggestions provided in this site but it didn't solve my issue.
  Am I making any mistake in the code?.Any help is appreciated.</p>