[英]HTTP call with Socks 4 proxy
我需要使用襪子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();
我可以通過在系統級別設置襪子代理
// 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。
這是SocksSocketImpl
實現中的錯誤:
這是我嘗試過的,似乎正在運行。 基本上,我需要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);
請分享您的意見,對這種方法的反饋。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.