如何向Jsoup添加代理支持? [英] How to add proxy support to Jsoup?

查看:176
本文介绍了如何向Jsoup添加代理支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的初学者,我的第一个任务是解析大约10,000个URL并从中提取一些信息,为此,我正在使用 Jsoup ,并且工作正常.

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.

但是现在我想为其添加代理支持.代理也有用户名和密码.

But now I want to add proxy support to it. The proxies have a username and password too.

推荐答案

您不必通过Jsoup获取网页数据.这是我的解决方案,虽然可能不是最好的.

You don't have to get the webpage data through Jsoup. Here's my solution, it may not be the best though.

  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);

  uc.connect();

    String line = null;
    StringBuffer tmp = new StringBuffer();
    BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
    while ((line = in.readLine()) != null) {
      tmp.append(line);
    }

    Document doc = Jsoup.parse(String.valueOf(tmp));

就在那里.这将通过代理获取html页面的源,然后使用Jsoup对其进行解析.

And there it is. This gets the source of the html page through a proxy and then parses it with Jsoup.

这篇关于如何向Jsoup添加代理支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆