2016-11-29 5 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");