Java 为什么我的 SOCKS 代理代码抛出 SocketException: Malformed reply from SOCKS server?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27215053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does my SOCKS proxy code throw SocketException: Malformed reply from SOCKS server?
提问by Pter
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?
为什么我的 SOCKS 代理代码会抛出SocketException: Malformed reply from SOCKS server
?我试过在 URLConnection 或其他中设置,但这不起作用。唯一有效的东西 - chilkat lib,但它是商业的。那么,例如,我如何通过 ASOCKS 代理发出 http 请求?也许存在一些免费的库?
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("google.com", 80);
try {
socket.connect(dest);
} catch (IOException ex) {
Logger.getLogger(CheckProxy.class.getName()).log(Level.SEVERE, null, ex);
}
Throws exception:
抛出异常:
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)
Where line 86 is "socket.connect(dest);"
其中第 86 行是“socket.connect(dest);”
采纳答案by Pter
Explanation of this bug: bugs.java.com/view_bug.do?bug_id=6964547
这个bug的解释:bugs.java.com/view_bug.do?bug_id=6964547
You need to manually change mode in Socket object using this code:
您需要使用以下代码手动更改 Socket 对象中的模式:
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) {
}
回答by user207421
SocketException: Malformed reply from SOCKS server
That indicates that you don't have a SOCKS proxy at all. Possibly it is an HTTP proxy.
这表明您根本没有 SOCKS 代理。可能它是一个 HTTP 代理。
And note that you asked the wrong question. Your code was correct, it was your premiss that was wrong.
并注意你问错了问题。你的代码是正确的,是你的前提是错误的。