Link to home
Start Free TrialLog in
Avatar of nak nak
nak nak

asked on

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.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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'
Avatar of nak nak
nak nak

ASKER

@CEHJ, I am able to print and see the values of PROXY_HOST  and PROXY_PORT and these have expected values.
As i said, your code does not set them
Avatar of nak nak

ASKER

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.
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
Avatar of nak nak

ASKER

PROXY_HOST = addr.getHostName(); PROXY_PORT = addr.getPort(); inside getProxyDetails(). I my workspace I have called this method inside main()
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nak nak

ASKER

@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.
For the benefit of future visitors, please post your revised code
Avatar of nak nak

ASKER

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

Avatar of nak nak

ASKER

line number 71 should be setProxyDetails(); not getProxyDetails();
    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)
https://www.experts-exchange.com/questions/29047620/Malformed-Reply-from-SOCKS-Server-when-fetching-proxy-server-details-dynamically.html?anchorAnswerId=42237133#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