0
我在这里有一个有趣的场景。我有一个代理服务器地址,每次向它发出HTTP请求时都应该为我提供一个新的退出IP。我注意到,退出IP只会在重启程序后才会改变,而不是每次循环迭代。以下是我的来源。重新建立与代理服务器的连接
循环调用getHTML每次迭代:
String result = getHTML("https://wtfismyip.com/text");
public static String getHTML(String urlToRead) throws Exception {
InetSocketAddress addy = new InetSocketAddress("example.proxy.com", 1234);
Proxy proxy = new Proxy(Proxy.Type.HTTP, addy);
StringBuilder result = new StringBuilder();
URL url = new URL(urlToRead);
HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
conn.setRequestMethod("GET");
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
rd.close();
conn.disconnect();
return result.toString();
}
结果将继续每次相同IP,直到我重新启动程序。我觉得像一些流或套接字还没有关闭,它保持连接活着。