我需要使用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(); 但我的其他连接,如连接到数据库,加密服务器也通过代理,并失败。
我还尝试从系统代理中排除服务器,但没有成功。
System.getProperties().put("socksnonProxyHosts","*.net");
System.getProperties().put("http.nonProxyHosts","*.net"); 在java1.6中有没有其他方式可以选择使用SOCKS4。
发布于 2014-02-13 08:01:51
这是SocksSocketImpl实现中的一个错误:
JDK-6964547 : Impossible to set useV4 in SocksSocketImpl
发布于 2014-02-14 01:45:34
这就是我尝试过的方法,看起来它是有效的。基本上,我需要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); 请分享您对此方法的评论和反馈。
https://stackoverflow.com/questions/21742103
复制相似问题