gpt4 book ai didi

java - HTMLUnit 的 WebClient 代理授权错误

转载 作者:行者123 更新时间:2023-12-04 18:05:23 26 4
gpt4 key购买 nike

我是 HTMLUnit 的新手。我尝试了一些示例以更好地理解 HTMLUnit。

我使用的是 HTMLUnit 2.15 版本。

代码:

    final WebClient webClient = new WebClient(BrowserVersion.getDefault(), "***.***.com", portNo);

// set proxy username and password
final DefaultCredentialsProvider credentialsProvider = (DefaultCredentialsProvider) webClient.getCredentialsProvider();
credentialsProvider.addCredentials(username, password);
//credentialsProvider.addNTLMCredentials(username, password, null, -1, "localhost", "domain");
HtmlPage page = null;
try {
page = webClient.getPage("http://htmlunit.sourceforge.net");
} catch (FailingHttpStatusCodeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println("Google" + page.getTitleText());

当我运行此代码时,我收到以下错误消息。

   com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException: 407 Proxy Authorization Required for http://htmlunit.sourceforge.net/
at com.gargoylesoftware.htmlunit.WebClient.throwFailingHttpStatusCodeExceptionIfNecessary(WebClient.java:527)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:352)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:407)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:392)
at Main.main(Main.java:46)
Exception in thread "main" java.lang.NullPointerException
at Main.main(Main.java:58)

我确实知道为什么在看到凭据后我会收到代理异常。

最佳答案

尝试直接在 WebClient 的凭据提供程序上设置凭据。这在 2.18 版中对我有用:

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;

import org.apache.http.auth.AuthScope;
import org.apache.http.auth.NTCredentials;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class App {

private static final String PROXY_HOST= "***.***.com";
private static final int PROXY_PORT=8080;
private static final String USERNAME = "username";
private static final String PASSWORD = "password";

public static void main(String...strings) throws FailingHttpStatusCodeException, MalformedURLException, IOException {

WebClient client = new WebClient(BrowserVersion.FIREFOX_38, PROXY_HOST, PROXY_PORT);
client.getCredentialsProvider().setCredentials(AuthScope.ANY, new NTCredentials(USERNAME, PASSWORD, "", ""));
HtmlPage page = client.getPage("http://google.com");
page.save(new File("google.html"));
client.close();
}

}

关于java - HTMLUnit 的 WebClient 代理授权错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27837102/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com