2015-01-08 76 views
1

我是HTMLUnit的新手。我已經嘗試了一些示例來更好地理解HTMLUnit。WebClient代理授權錯誤與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) 

我知道爲什麼我看到證書後得到代理異常。

+0

我會嘗試舊版本的HTMLUnit看看會發生什麼。這可能是一個錯誤。試試HTMLUnit 2.13 – Arya

回答

0

你可以嘗試使用:

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

請爲proxyHostName提供IP,你可以讓你的工作站:

workstation = System.getenv("COMPUTERNAME"); 

而且反正你有在您的NT憑據使用正確的域名

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(); 
    } 

}