Pter Pter - 1 year ago 124
HTTP Question

Why does my SOCKS proxy code throw SocketException: Malformed reply from SOCKS server?

Why does my SOCKS proxy code throw

SocketException: Malformed reply from SOCKS server
? I've tried to set in URLConnection or other, but this doesn't work. Only thing that worked - chilkat lib, but it's commercial. So, how I, for example, make http request through a ASOCKS proxy? Maybe exist some free lib for that?

For example, that code:

SocketAddress addr = new InetSocketAddress(proxy_ip, proxy_port);
Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr);
Socket socket = new Socket(proxy);
InetSocketAddress dest = new InetSocketAddress("", 80);
try {
} catch (IOException ex) {
Logger.getLogger(CheckProxy.class.getName()).log(Level.SEVERE, null, ex);

Throws exception: Malformed reply from SOCKS server
at Source)
at Source)
at Source)
at Source)
at proxychecker.CheckProxy.SocksCheck(

Where line 86 is "socket.connect(dest);"

Answer Source

Explanation of this bug:

You need to manually change mode in Socket object using this code:

Class clazzSocks  = socket.getClass();
Method setSockVersion  = null;
Field sockImplField = null; 
SocketImpl socksimpl = null; 
try {
  sockImplField = clazzSocks.getDeclaredField("impl");
  socksimpl  = (SocketImpl) sockImplField.get(socket);
  Class clazzSocksImpl  =  socksimpl.getClass();
  setSockVersion  = clazzSocksImpl.getDeclaredMethod("setV4");
  if(null != setSockVersion){
  sockImplField.set(socket, socksimpl);
} catch (Exception e) {

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download