我正在尝试创建一个计算机化的机器人,通过 HTTP 连接通过不同的 IP(主要是通过代理)访问特定网站。
我根据以下代码创建了脚本
System.getProperties().put( "proxySet", "true" );
System.setProperty("http.proxyHost", "<PROXY IP>");
System.setProperty("http.proxyPort", "<PORT>");
ourURL = new URL("<TARGET WEBSITE>");
huc = (HttpURLConnection) ourURL.openConnection();
huc.setRequestMethod("GET");
但是上面代码中的问题是,当我检查引用网址时,它只需要我的公共(public) IP,而不需要代理 IP。有人可以帮我解决这个问题吗?
最佳答案
从 java 1.5 开始,您需要在 openConnection 之前使用类似于下面的代码进行代理:
System.setProperty("http.proxyHost", "proxy.****.com");
System.setProperty("http.proxyPort", "####");
Authenticator authenticator = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return (new PasswordAuthentication("user",
"pwd".toCharArray()));
}
};
Authenticator.setDefault(authenticator);
关于java - 如何通过java程序通过代理连接网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23422300/