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.httpcli ent.Creden tials;
import org.apache.commons.httpcli ent.HostCo nfiguratio n;
import org.apache.commons.httpcli ent.HttpCl ient;
import org.apache.commons.httpcli ent.HttpMe thod;
import org.apache.commons.httpcli ent.HttpSt atus;
import org.apache.commons.httpcli ent.Userna mePassword Credential s;
import org.apache.commons.httpcli ent.auth.A uthScope;
import org.apache.commons.httpcli ent.method s.GetMetho d;
import java.io.IOException;
import java.net.InetSocketAddress ;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URISyntaxExceptio n;
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.getHostConfiguratio n();
config.setProxy(PROXY_HOST , PROXY_PORT);
try {
client.executeMethod(metho d);
if (method.getStatusCode() == HttpStatus.SC_OK) {
String response = method.getResponseBodyAsSt ring();
System.out.println("Respon se = " + 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("proxyS et", "true");
try {
Proxy proxy = (Proxy) ProxySelector.getDefault() .select(ne w URI(
"https://www.example2.com/xyz")).iterator().
next();
System.out.println("proxy hostname : " + proxy.type());
InetSocketAddress addr = (InetSocketAddress)proxy.a ddress();
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.
This is the code which is working fine:
import org.apache.commons.httpcli
import org.apache.commons.httpcli
import org.apache.commons.httpcli
import org.apache.commons.httpcli
import org.apache.commons.httpcli
import org.apache.commons.httpcli
import org.apache.commons.httpcli
import org.apache.commons.httpcli
import java.io.IOException;
import java.net.InetSocketAddress
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URISyntaxExceptio
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.getHostConfiguratio
config.setProxy(PROXY_HOST
try {
client.executeMethod(metho
if (method.getStatusCode() == HttpStatus.SC_OK) {
String response = method.getResponseBodyAsSt
System.out.println("Respon
}
} 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("proxyS
try {
Proxy proxy = (Proxy) ProxySelector.getDefault()
"https://www.example2.com/xyz")).iterator().
next();
System.out.println("proxy hostname : " + proxy.type());
InetSocketAddress addr = (InetSocketAddress)proxy.a
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.
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
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
ASKER
PROXY_HOST = addr.getHostName(); PROXY_PORT = addr.getPort(); inside getProxyDetails(). I my workspace I have called this method inside main()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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();
}
}
}
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/HttpURLConnecti
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
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