我需要使用socks 4代理调用服务器。我在java版本1.6上。
如果我们使用类似的东西,那么它会将SOCKS代理视为版本5.
URL url = new URL("https://www.google.com");
URLConnection connection = null;
SocketAddress proxySocketAddress1 = new InetSocketAddress("XXXXXXXXXX", 8081);
Proxy proxy = new Proxy(Proxy.Type.SOCKS, proxySocketAddress1);
connection = url.openConnection(proxy);
connection.setConnectTimeout(150000);
connection.connect();
我可以通过
在系统级别设置socks代理// Set SOCKS proxy
System.getProperties().put( "socksProxyHost","xxxxx");
System.getProperties().put( "socksProxyPort","1234");
System.getProperties().put("socksProxyVersion","4");
当我这样做时,我能够到达服务器
connection = url.openConnection();
但我的其他连接如连接到db,加密服务器也通过代理并失败。
我也试过从系统代理中排除服务器但没有成功。
System.getProperties().put("socksnonProxyHosts","*.net");
System.getProperties().put("http.nonProxyHosts","*.net");
我还有其他方法可以选择在java 1.6中使用SOCKS4。
答案 0 :(得分:0)
这是SocksSocketImpl
实施中的错误:
答案 1 :(得分:0)
这是我尝试过的,看起来它正在发挥作用。基本上我需要SOCKS4代理连接到套接字。
SocketAddress socketAddress = new InetSocketAddress("proxyhost",proxyport);
Proxy socketProxy = new Proxy(Proxy.Type.SOCKS, socketAddress);
Socket socket = new Socket(socketProxy);
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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String hostName="xxxxx";
int port=1080;
InetAddress address;
SocketAddress socketAddress;
address = InetAddress.getByName(hostName);
socketAddress = new InetSocketAddress(address, port);
// Connect to socket
socket.connect(socketAddress, 100000);
//setting the socket read() connection time out
socket.setSoTimeout(100000);
请分享您的意见,对此方法的反馈。