2014-11-06 107 views
0

我试图通过代理使用JSoup。它的工作很好,当我只使用一个新的代理(http://incloak.com/proxy-list/),但当我改变一个好的IP即107.165.33.12:3128 - > 10 .165.33.12:3128它不应该工作,因为IP不是一个现有的代理。但它仍然只通过我自己的IP地址获取页面。Jsoup设置代理

我能做些什么来解决这个问题,我想在代理不工作时得到某种错误。

useProxy.java

public class useProxy { 

    public static void main(String[] args) throws IOException { 

     System.setProperty("http.proxyHost", "107.165.33.12"); 
     System.setProperty("http.proxyPort", "3128"); 
     Document doc = Jsoup.connect("http://www.toolsvoid.com/what-is-my-ip-address") //http://goldenpirates.org/proxy/azenv.php 
          .ignoreContentType(true) 
          .userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0") 
          .referrer("http://www.google.nl/") 
          .timeout(12000) 
          .followRedirects(true) 
          .header("Accept-Language", "en") 
          .header("Accept-Encoding","gzip,deflate,sdch") 
          .get(); 

     String iP = doc.select("table.list").select("strong").first().text(); //get used IP. 
     String info = doc.select("textarea").text(); //get used IP. 


     System.out.println("IP-Adres: " + iP); 
     System.out.println("Info: \n" + info); 
    } 

} 

回答

0

你可能要像做这种方式....

final String authUser = "USERNAME"; 
    final String authPassword = "PASSWORD"; 



    Authenticator.setDefault(
        new Authenticator() { 
         public PasswordAuthentication getPasswordAuthentication() { 
         return new PasswordAuthentication(
           authUser, authPassword.toCharArray()); 
         } 
        } 
       ); 

    .. 

    System.setProperty("http.proxyHost", "192.168.5.1"); 
    System.setProperty("http.proxyPort", "1080"); 
    .. 
+0

它会抛出什么错误? – Pavlo 2016-10-27 13:49:01