3

如何使用 HtmlUnitDriver 处理身份验证?

4

2 回答 2

2

如果这是您需要的基本身份验证,您可以在创建 HtmlUnitDriver 实例时执行此操作:(代码在 scala 中,但您可以轻松地将其更改为 java)

new HtmlUnitDriver() {
  override def modifyWebClient(client: WebClient) = {
    val creds = new DefaultCredentialsProvider()
    creds.addCredentials("user-name", "user-password");
    client.setCredentialsProvider(creds)
    client
  }
} 
于 2012-10-25T12:15:09.753 回答
2

在java中尝试这个似乎对我有用

WebDriver driver = new HtmlUnitDriver() {
    protected WebClient modifyWebClient(WebClient client) {
        // This class ships with HtmlUnit itself
        DefaultCredentialsProvider creds = new DefaultCredentialsProvider();

        // Set some example credentials
        creds.addCredentials("username", "password");

        // And now add the provider to the webClient instance
        client.setCredentialsProvider(creds);

        return client;
    }
};
于 2013-04-07T11:11:10.723 回答