Java proxy-vole pac代理访问身份验证错误

时间:2019-05-07 20:26:39

标签: java proxy jsoup

我使用proxy-vole库通过pac proxy连接到url。 我总是收到验证错误

java.io.IOException:无法通过代理隧道传输。代理返回“需要HTTP / 1.1 407代理授权”

这是我的代码,请告诉我该如何解决

public static void main(String[] args) {
    UrlPacScriptSource source = new UrlPacScriptSource("https://d1tzjzeluf7kb7.cloudfront.net/pacs/us-wa.pac");
    PacProxySelector proxySelector = new PacProxySelector(source);
    ProxySelector.setDefault(proxySelector);

    URI home = URI.create("http://www.google.com");
    System.out.println("ProxySelector: " + proxySelector);
    System.out.println("URI: " + home);
    List<Proxy> proxyList = proxySelector.select(home);
    if (proxyList != null && !proxyList.isEmpty()) {
        for (Proxy proxy : proxyList) {
            System.out.println(proxy);
            SocketAddress address = proxy.address();
            if (address instanceof InetSocketAddress) {
                try {
                    ProxyData proxyData = new ProxyData();
                    proxyData.setHostName(((InetSocketAddress) address).getHostString());
                    proxyData.setPort(((InetSocketAddress) address).getPort());

                    Authenticator.setDefault(new Authenticator() {
                        @Override
                        protected PasswordAuthentication getPasswordAuthentication() {
                            if (getRequestorType() == RequestorType.PROXY) {
                                return new PasswordAuthentication("cucungmatran", "Changeguy1!".toCharArray());
                            } else {
                                return super.getPasswordAuthentication();
                            }
                        }
                    });
                    System.setProperty("http.proxyHost", proxyData.getHostName());
                    System.setProperty("http.proxyPort", proxyData.getPort()+"");
                    System.out.println(Jsoup.connect("https://ipinfo.info/html/ip_checker.php").get().select("#Text14 > p > span > a > b").text());
                } catch (IOException ex) {
                    Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在获取结果之前,应将当前代理应用于Jsoup连接:

Jsoup.connect("https://ipinfo.info/html/ip_checker.php")
        .proxy(proxy)
        .get().select("#Text14 > p > span > a > b").text();