HTMLUnit的WebClient代理授权错误

时间:2015-01-08 09:47:35

标签: java htmlunit

我是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)

我知道为什么在看到凭证后我收到代理异常。

3 个答案:

答案 0 :(得分:0)

您可以尝试使用:

credentialsProvider.addNTLMCredentials(userName, password, proxyHostName, Integer.parseInt(proxyPort), workstation, domain);

请为proxyHostName提供ip,您可以通过以下方式获取工作站:

workstation = System.getenv("COMPUTERNAME");

无论如何,你必须在你的NT凭证所在的地方使用正确的域名。

答案 1 :(得分:0)

尝试直接在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();
    }

}

答案 2 :(得分:0)

我最近也做了同样的事情。 这将使用WebClient上的DefaultCredentialsProvider。

DefaultCredentialsProvider credentialsProvider = new DefaultCredentialsProvider();
        ProxyConfig proxyConfig = new ProxyConfig(proxy.getAddress(), proxy.getPort());
            try (WebClient client = new WebClient()) {
            if (!proxy.getUserName().isEmpty()) {
                credentialsProvider.addCredentials(proxy.getUserName(), proxy.getPassword());
                client.setCredentialsProvider(credentialsProvider);
            }
                client.getOptions().setThrowExceptionOnFailingStatusCode(false);
                client.getOptions().setThrowExceptionOnScriptError(false);
                client.getOptions().setUseInsecureSSL(true);

                /* Clearing Cache and Cookies */
                client.getCookieManager().clearCookies();
                client.getCache().clear();

                /* Setting proxy to be used */
                client.getOptions().setProxyConfig(proxyConfig);

                /* Actual Web Page LOAD */
                Page startPage = client.getPage(url);

                            /* Create ProxyStatus record */
                WebResponse response = startPage.getWebResponse();
                }catch (Exception e) {
                    e.printStackTrace();
                }