Malformed Reply from SOCKS Server when fetching proxy server details dynamically

I am trying to access a website through proxy server.I am using httpclient.

This is the code which is working fine:

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");
    }
}

But When I use following method to set PROXY_HOST and PROXY_PORT values dynamically I am getting "Malformed reply from SOCKS server" exception

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();
    }
}

I tried other suggestions provided on the internet but it didn't solve my issue. Am I making any mistake in the code?.Any help is appreciated.
nak nakAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
But When I use following method to set PROXY_HOST and PROXY_PORT values dynamically
Those values are not being set at all in the (misleadingly-named) method 'getProxyDetails'
0
nak nakAuthor Commented:
@CEHJ, I am able to print and see the values of PROXY_HOST  and PROXY_PORT and these have expected values.
0
CEHJCommented:
As i said, your code does not set them
0
Webinar: Miercom Evaluates Wi-Fi Security

It's not just about Wi-Fi connectivity anymore. A wireless security breach can cost your business large amounts of time, trouble, and expense. Plus, hear first-hand from Miercom how WatchGuard's Wi-Fi security stacks up against the competition in our upcoming webinar!

nak nakAuthor Commented:
In the question I have not included code to call the getProxyDetails() method but I have mentioned I am calling the said method. In my workspace I am calling getProxyDetails() from main() and then printing the values of PROXY_HOST and PROXY_PORT, these have expected values. now when I pass  PROXY_HOST and PROXY_PORT to setProxy() and then try to call client.execute(method) the execute()  is throwing said exception. but if i hard code host and port values I am not getting the exception and the code works as expected. I don't know why i am getting the exception when I am not hard coding the values.
0
CEHJCommented:
Let's try it this way: please show me the lines in the above code, where you SET those properties. You can just quote the two lines
0
nak nakAuthor Commented:
PROXY_HOST = addr.getHostName(); PROXY_PORT = addr.getPort(); inside getProxyDetails(). I my workspace I have called this method inside main()
0
CEHJCommented:
a. that's setting static variables in your class, not networking properties
b. i don't see any sign of getProxyDetails() being called at all

I'd
a. rename the method setProxyDetails (that's its intent)
b. pass that method an instance of HostConfiguration and use that parameter
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nak nakAuthor Commented:
@CEHJ passing HostConfiguration reference to the method doesn't fix the problem either. Was able to solve the problem using HttpURLConnection instead of HttpClient. Thanks for your time.
0
CEHJCommented:
For the benefit of future visitors, please post your revised code
0
nak nakAuthor Commented:
This is the working code using HttpURLConnection :

package com.antares;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Properties;

import javax.net.ssl.HttpsURLConnection;

public class URLWithProxy {
	public static String Proxy = "";
	public static int port = 0;
	
	public static void setProxyDetails()
    {
    	System.setProperty("java.net.useSystemProxies", "true");
        Properties systemSettings = System.getProperties();
        systemSettings.put("proxySet", "true");
        try {
			Proxy proxy = (Proxy) ProxySelector.getDefault().select(new URI(
			        "https://www.dummy.in")).iterator().
			     next();
			 System.out.println("proxy hostname : " + proxy.type());
		      InetSocketAddress addr = (InetSocketAddress)proxy.address();
		      if (addr == null) {
		          System.out.println("No Proxy");
		       } else {
		    	   Proxy = addr.getHostName();
		    	   port = addr.getPort();
		    	   
		    	   
		       }
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

	 private static void print_content(HttpsURLConnection con){
			if(con!=null){

			try {

			   System.out.println("****** Content of the URL ********");
			   BufferedReader br =
				new BufferedReader(
					new InputStreamReader(con.getInputStream()));

			   String input;

			   while ((input = br.readLine()) != null){
			      System.out.println(input);
			   }
			   br.close();

			} catch (IOException e) {
			   e.printStackTrace();
			}

		       }
	 }
	 
	public static void main(String[] args) {
		getProxyDetails();
		
		InetSocketAddress proxyInet = new InetSocketAddress(Proxy,port);
		Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP, proxyInet);		
		 URL url=null;
	     String sUrl = "https://www.dummy.in";
	     try {
	     url = new URL(sUrl);
	     try {
			HttpsURLConnection conn = (HttpsURLConnection)url.openConnection(proxy);
			print_content(conn);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	     
	     } catch (MalformedURLException e) {
	     // TODO Auto-generated catch block
	     e.printStackTrace();
	     }
	    	     
	}

}

Open in new window

0
nak nakAuthor Commented:
line number 71 should be setProxyDetails(); not getProxyDetails();
0
CEHJCommented:
    setProxyDetails();

        InetSocketAddress proxyInet = new InetSocketAddress(Proxy, port
);

That's the reason your code now works (nothing to do with HttpClient/HttpURLConnection): you now use the variables after setting them, which you were not doing before (as i told you)
0
CEHJCommented:
https://www.experts-exchange.com/questions/29047620/Malformed-Reply-from-SOCKS-Server-when-fetching-proxy-server-details-dynamically.html#a42237133
contains the best answer. It's conceivable i didn't express myself clearly, but for the record: the OP was setting the variables, but those were not propagated to the actual networking properties concerned
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.