我在多个 NIC 上配置了一个公共 IP 地址池。
在我的 JAVA 项目中,该项目在 LINUX 计算机上运行,我需要从池中选择一个特定的 IP 地址,并使用该 IP 创建一个 HttpURLConnecion。此外,我将在池上循环,每次使用不同的 ip。
在现阶段,我无法找到使用 java.net 库的解决方案。我更关注的是 Apache 的 HttpClient。在以下链接中,http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html,据说这样的库可以支持我正在寻找的功能。有关此问题的讨论可以在以下位置找到:使用 Apache HttpClient 定义源 IP 地址 https://stackoverflow.com/questions/8213714/define-source-ip-address-using-apache-httpclient。实际上,发布的线程似乎不是结论性的,因为用户的体验与所描述的库的使用形成鲜明对比。
因此,我不认为SO社区真正成功地解决了这个问题。事实上,在 SO 上可以找到关于这个主题的几个重播的问题/答案,但似乎没有一个对问题进行详尽的分析。
此外,使用 java.net 库(如在我的项目中)根本不会遇到问题。
目前,我可能的选择是调用一些 LINUX 系统命令(来自 java)来切换用于当前连接的 NIC。不过,我还没有弄清楚。
因此,如果任何在解决此问题方面有积极经验的用户能够向我提出解决方案/想法/方法,我将不胜感激。
提前致谢,
Marcello
UPDATE:
我目前已经实现了这个测试代码。它给了我正确的状态代码(200)。不过需要用多个ip地址进行测试。
public class test {
public static void main(String[] args) {
final String authUser = "admin";
final String authPassword = "password";
Authenticator.setDefault(
new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
authUser, authPassword.toCharArray());
}
}
);
System.setProperty("http.proxyUser", authUser);
System.setProperty("http.proxyPassword", authPassword);
try {
Properties systemProperties = System.getProperties();
URL url = new URL("yourURL");
systemProperties.setProperty("http.proxyHost","localhost");
systemProperties.setProperty("http.proxyPort", "8080");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int status = connection.getResponseCode();
System.out.println(status);
} catch (IOException e) {
System.out.println("connection problems");
}
}
}
此时,您应该能够配置与每个网卡相关的不同 TCP 端口。有人尝试过这样的事情吗?
我期待阅读新的想法/评论。
更新2:准确地说,我已经为那些需要它的人提供了身份验证设置。