How to add proxy support to Jsoup?

前端 未结 7 1720
独厮守ぢ
独厮守ぢ 2020-11-28 03:47

I am a beginner to Java and my first task is to parse some 10,000 URLs and extract some info out of it, for this I am using Jsoup and it\'s working fine.

7条回答
  •  独厮守ぢ
    2020-11-28 04:37

    Try this code instead:

    URL url = new URL("http://www.example.com/");
    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8080)); // or whatever your proxy is
    
    HttpURLConnection uc = (HttpURLConnection)url.openConnection(proxy);
    hc.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
    uc.setRequestProperty("Content-Language", "en-US");
    uc.setRequestMethod("GET");
    uc.connect();
    
    Document doc = Jsoup.parse(uc.getInputStream());
    

提交回复
热议问题