2016-11-29 57 views
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,直到我重新启动程序。我觉得像一些流或套接字还没有关闭,它保持连接活着。

回答

0

找到了我的问题的答案。 TCP套接字保持活动状态,并允许它在不重新连接的情况下保持与代理的隧道连接。

我需要在代码的某处添加此语句,我把它放在这个类初始化的开始处。

System.setProperty("http.keepAlive", "false"); 
相关问题