gpt4 book ai didi

java - 为什么我的 SOCKS 代理代码抛出 SocketException : Malformed reply from SOCKS server?

转载 作者:可可西里 更新时间:2023-11-01 16:05:10 41 4
gpt4 key购买 nike

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

例如,代码:

    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);”

最佳答案

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

您需要使用此代码手动更改 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) {

}

关于java - 为什么我的 SOCKS 代理代码抛出 SocketException : Malformed reply from SOCKS server?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27215053/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com