java - 失败的 HttpStatusCodeException : 401 Unauthorized acess using HtmlUnit

标签 java http webclient htmlunit

我正在尝试使用以下代码连接到使用 HtmlUnit 的页面:

WebClient webClient = new WebClient();
webClient.getOptions().setThrowExceptionOnScriptError(false);
HtmlPage loginPage = webClient.getPage(WEBSPHERE_URL);

但我总是收到这个错误:

18:06:00,390 ERROR [stderr] (Refresh Thread: Equinox Container: 90609616-a614-0017-138e-e6e4aa4a0871) com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException: 401 Unauthorized for https://portal-intranet.ti.sabesp.com.br/wps/myportal
18:06:00,390 ERROR [stderr] (Refresh Thread: Equinox Container: 90609616-a614-0017-138e-e6e4aa4a0871)   at com.gargoylesoftware.htmlunit.WebClient.throwFailingHttpStatusCodeExceptionIfNecessary(WebClient.java:576)
18:06:00,390 ERROR [stderr] (Refresh Thread: Equinox Container: 90609616-a614-0017-138e-e6e4aa4a0871)   at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:401)

当我尝试使用浏览器访问 URL 时,它运行良好。我应该怎么做才能解决这个问题?

更新

现在我收到这个错误:

21:43:35,746 ERROR [osgi.logging.setup_environment] (Thread-257) [br.com.sabesp.setupenvironment.portlet.SetupEnvironmentAction(2427)] The activate method has thrown an exception : java.lang.LinkageError: loader constraint violation: when resolving method "com.gargoylesoftware.htmlunit.WebClient.setCredentialsProvider(Lorg/apache/http/client/CredentialsProvider;)V" the class loader (instance of org/eclipse/osgi/internal/loader/EquinoxClassLoader) of the current class, br/com/sabesp/setupenvironment/importer/util/WebContentUtil, and the class loader (instance of org/eclipse/osgi/internal/loader/EquinoxClassLoader) for the method's defining class, com/gargoylesoftware/htmlunit/WebClient, have different Class objects for the type org/apache/http/client/CredentialsProvider used in the signature

当我尝试这样做时:

DefaultCredentialsProvider userCredentials = new DefaultCredentialsProvider();
userCredentials.addCredentials("username", "password");
webClient.setCredentialsProvider(userCredentials);

我正在使用基于 OSGi 框架的 Liferay DXP,我也在使用 Gradle。

最佳答案

你似乎是针对一个 Intranet 网站运行的,我猜这可以从浏览器中检测到你的用户名。

通过 HtmlUnit,您可以使用 NTLM 身份验证:

DefaultCredentialsProvider credentialsProvider =
                (DefaultCredentialsProvider) webClient.getCredentialsProvider();
credentialsProvider.addCredentials("username", "password");
//OR
credentialsProvider.addNTLMCredentials("username", "password", null, -1, "localhost", "domain");

您还可以启用流量日志以查看需要哪种类型的身份验证:

LogManager.getLogger("org.apache.http.wire").setLevel(Level.ALL);

更新:

您似乎有冲突的 httpclient jar,请删除其中一个。

下面的代码会告诉正在使用的是哪一个:

System.out.println(CredentialsProvider.class.getProtectionDomain()
    .getCodeSource().getLocation());

关于java - 失败的 HttpStatusCodeException : 401 Unauthorized acess using HtmlUnit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43101058/

相关文章:

angular - 如何在 Angular 5 中从 http 重定向到 https?

java - 自动生成加特林场景

swift - PFFile url 返回以 http 而不是 https 开头的 url

http - Google 是否可以索引 HTTP GET 变量

c# - 如何在 C# 中为 webclient 设置端口?

c# - 使用 WebClient 或 HttpWebRequest 登录

java - 如何让 XStream 解析来自 StAX 的部分输入

java - 为什么 .and().httpBasic();不适用于 WebSecurityConfigurerAdapter

java - key 'string1-string2' 的重复条目 'PRIMARY'

c# - WebClient UploadData 在发布带有扩展 ascii 字符的 json 数据时出现 400 Bad Request 错误