【问题标题】:FailingHttpStatusCodeException: 401 Unauthorized acess using HtmlUnitHttpStatusCodeException 失败:401 Unauthorized access using HtmlUnit
【发布时间】:2017-03-29 18:11:33
【问题描述】:

我正在尝试使用此代码使用 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。

【问题讨论】:

    标签: java http webclient htmlunit


    【解决方案1】:

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

    使用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.lang.LinkageError:加载程序约束冲突:解析方法“com.gargoylesoftware.htmlunit.WebClient. setCredentialsProvider(Lorg/apache/http/client/CredentialsProvider;)V" 当前类的类加载器(org/eclipse/osgi/internal/loader/EquinoxClassLoader的实例),br/com/sabesp/setupenvironment/importer/util/ WebContentUtil,
    • 和方法的定义类 com/gargoylesoftware/htmlunit/WebClient 的类加载器(org/eclipse/osgi/internal/loader/EquinoxClassLoader 的实例)对于 org/ 类型具有不同的 Class 对象签名中使用的apache/http/client/CredentialsProvider
    • 我遇到了同样的错误。我不知道是不是因为我的应用程序是基于 OSGi 的,也许 html 正在使用另一种 httpclient jar 类型。
    • 我使用的是 httpclient-osgi-4.5.3.jar 和 org.apache.httpcomponents.httpcore_4.4.6.jar,它们都是基于 osgi 的。
    猜你喜欢
    • 2018-02-20
    • 2022-11-17
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 2022-12-28
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多