有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将httpClient配置为jsoup

我试图将http客户机设置为Jsoup,以使用TOR作为代理,但我找不到将客户机设置为Jsoup的方法,我想知道在知道Jsoup不需要httpClient配置的情况下如何才能做到这一点


共 (1) 个答案

  1. # 1 楼答案

    Jsoup不使用HttpClient,但您可以使用以下方法设置代理:

    // Setup proxy
    Proxy proxy = new Proxy(Proxy.Type.HTTP,
            InetSocketAddress.createUnresolved("127.0.0.1", 80));
    
    // Setup the authenticator
    Authenticator authenticator = new Authenticator() {
    
        public PasswordAuthentication getPasswordAuthentication() {
            return (new PasswordAuthentication("user",
                    "password".toCharArray()));
        }
    };
    Authenticator.setDefault(authenticator);
    
    // Send a request using the proxy
    Document doc = Jsoup.connect("http://www.example.com/")
            .proxy(proxy)
            .get();