如何将带有用户名和密码的代理添加到Jsoup?

问题描述

我正在开发一个Java应用程序,该应用程序需要通过代理(使用用户名和密码)连接到Internet。但这是行不通的。有人可以帮我解决这个问题吗?我的Java代码如下:

    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.123.12.123");
    System.setProperty("http.proxyPort","1234");

    Document doc = null;
    try {
        doc = Jsoup 
                .connect("https://www.google.com").timeout(60000)
                .header("Content-Language","en-US")
                .get();

    } catch (IOException e) {
        e.printstacktrace();
    }

解决方法

您可以尝试以下方法:

System.setProperty("http.proxyUser","username");
System.setProperty("http.proxyPassword","password");

System.setProperty("https.proxyUser","username");
System.setProperty("https.proxyPassword","password");
,

我遇到了这个错误:

java.io.IOException: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 407 Proxy Authentication Required"
at java.base/sun.net.www.protocol.http.HttpURLConnection.doTunneling(HttpURLConnection.java:2180)
at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:169)
at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:142)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:732)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:707)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:297)
at org.jsoup.helper.HttpConnection.get(HttpConnection.java:286)
at com.html.proxy.MyProxySelector.connectProxy(MyProxySelector.java:46)
at com.html.proxy.MyProxySelector.main(MyProxySelector.java:56)

我想通过代理访问网页(例如gooogle.com)(例如:“ 192.123.12.123:123”,并使用用户名和密码)