I am trying to connect to a server through proxy. Below is the code I am using to set the proxy.
try {
System.setProperty("java.net.useSystemProxies", "true");
System.out.println("detecting proxies");
List l = null;
try {
l = ProxySelector.getDefault().select(new URI("https://www.google.com"));
} catch (URISyntaxException e) {
e.printStackTrace();
System.out.println("Failed to list the proxy..." + e.getMessage());
}
if (l != null) {
for (Iterator iter = l.iterator(); iter.hasNext();) {
java.net.Proxy proxy = (java.net.Proxy) iter.next();
System.out.println("proxy type: " + proxy.type());
InetSocketAddress addr = (InetSocketAddress) proxy.address();
if (addr == null) {
System.out.println("No Proxy");
} else {
System.out.println("proxy hostname: " + addr.getHostName());
System.setProperty("http.proxyHost", addr.getHostName());
System.setProperty("https.proxyHost", addr.getHostName());
System.out.println("proxy port: " + addr.getPort());
System.setProperty("http.proxyPort", Integer.toString(addr.getPort()));
System.setProperty("https.proxyPort", Integer.toString(addr.getPort()));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
And I am using the below code to send the server requests.
RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonValue);
Request request = new Request.Builder().url(settings.getServiceUrl() + "/" + service).addHeader("Authorization", Credentials.basic(settings.getServiceUserName(), settings.getServicePassword())).post(body).build();
OkHttpClient.Builder builder =new OkHttpClient.Builder();
OkHttpClient client = builder.readTimeout(6000 * 1000, TimeUnit.MILLISECONDS)
.writeTimeout(6000 * 1000, TimeUnit.MILLISECONDS).build();
Call call = client.newCall(request);
Response response = call.execute();
And I am getting the below error message.
java.net.SocketException: Malformed reply from SOCKS server
at java.net.SocksSocketImpl.readSocksReply(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at okhttp3.internal.platform.Platform.connectSocket(Platform.java:130)
at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:263)
at okhttp3.internal.connection.RealConnection.connectTunnel(RealConnection.java:235)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:177)
at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.java:224)
Any idea on how to resolve this ?