为什么我的 SOCKS 代理代码抛出 SocketException: Malformed reply from SOCKS server? [英] Why does my SOCKS proxy code throw SocketException: Malformed reply from SOCKS server?

查看:203
本文介绍了为什么我的 SOCKS 代理代码抛出 SocketException: Malformed reply from SOCKS server?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的 SOCKS 代理代码会抛出 SocketException: Malformed reply from SOCKS server?我试过在 URLConnection 或其他中设置,但这不起作用.唯一有效的东西 - chilkat lib,但它是商业的.那么,例如,我如何通过 ASOCKS 代理发出 http 请求?也许存在一些免费的库?

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?

例如,该代码:

    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("google.com", 80);
    try {
        socket.connect(dest);
    } catch (IOException ex) {
        Logger.getLogger(CheckProxy.class.getName()).log(Level.SEVERE, null, ex);
    }

抛出异常:

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 java.net.Socket.connect(Unknown Source)
at proxychecker.CheckProxy.SocksCheck(CheckProxy.java:86)

其中第 86 行是socket.connect(dest);"

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

推荐答案

此错误的说明:bugs.java.com/view_bug.do?bug_id=6964547

您需要使用以下代码手动更改 Socket 对象中的模式:

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");
  sockImplField.setAccessible(true);
  socksimpl  = (SocketImpl) sockImplField.get(socket);
  Class clazzSocksImpl  =  socksimpl.getClass();
  setSockVersion  = clazzSocksImpl.getDeclaredMethod("setV4");
  setSockVersion.setAccessible(true);
  if(null != setSockVersion){
      setSockVersion.invoke(socksimpl);
  }
  sockImplField.set(socket, socksimpl);
} catch (Exception e) {

} 

这篇关于为什么我的 SOCKS 代理代码抛出 SocketException: Malformed reply from SOCKS server?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆