Java + HtmlUnit WebClient + SSL 页面

标签 java ssl webclient htmlunit

我在使用 htmlUnit WebDriver 解析 HTML 页面时遇到一些问题。 我也不异常(exception)。 我的代码如下所示:

   public static void main(String[] args)
        throws FailingHttpStatusCodeException, MalformedURLException,
        IOException {

       WebClient wc = initWebClient();
       HtmlPage page = wc.getPage(Constants.START_PAGE);

       HtmlTextInput userInput = (HtmlTextInput) page
            .getElementById(Constants.INPUT_USERNAME_ID);
       userInput.setText(Constants.USERNAME_VALUE);

       HtmlPasswordInput passwordInput = (HtmlPasswordInput) page
            .getElementById(Constants.INPUT_PASSWORD_ID);
       passwordInput.setText(Constants.PASSWORD_VALUE);

       // get submit button
       HtmlSubmitInput submitButton = (HtmlSubmitInput) page
            .getElementById(Constants.SUBMIT_BUTTON_ID);

       HtmlPage afterLoginPage = submitButton.click();
       System.out.println(afterLoginPage.asXml());

       // some further processing
       ....
    }

    private static WebClient initWebClient(){
       WebClient wc = new WebClient(BrowserVersion.CHROME);
       wc.getCookieManager().setCookiesEnabled(true);
       wc.getOptions().setJavaScriptEnabled(false);
       wc.getOptions().setThrowExceptionOnScriptError(true);

       System.out.println("USE SSL");
       wc.getOptions().setUseInsecureSSL(true);

       return wc;
   }

在上面的 XML 页面源代码中,我可以找到我的名字。所以,看来我的记录是正确的。

如果我记录了它,那么我想通过在 WebClient 中设置它的 url 来转到页面。

    for(some loop){
            // create new WebClient, because it is MULTITHREADING processing. WebDriver is not thread safe so I need to create new WebClient for every thread
            WebClient wc = initWebClient();
            String url = "https://website/details/31944";
            HtmlPage detailsPage = wc.getPage(url);

            System.out.println(url);
            System.out.println(detailsPage.getUrl());
     }

以上系统输出将返回:

      https://website/details/31944
      http://website/details/31944

这意味着当我去 https://website/details/31944 , 我得到 http://website/details/31944 , 所以我不再登录了。

当我创建新的 WebClient 时,是否可以通过任何方式传递 SSL session ? 或者使用 WebClient 进行多线程处理的任何其他方法?

最好的问候,DS

最佳答案

我已经使用 cookie 解决了这个问题。

    public static void main(String[] args)
        throws FailingHttpStatusCodeException, MalformedURLException,
        IOException {

        WebClient wc = initWebClient(null);

        HtmlPage page = wc.getPage(Constants.START_PAGE);

        HtmlTextInput userInput = (HtmlTextInput) page
            .getElementById(Constants.INPUT_USERNAME_ID);
        userInput.setText(Constants.USERNAME_VALUE);

        HtmlPasswordInput passwordInput = (HtmlPasswordInput) page
            .getElementById(Constants.INPUT_PASSWORD_ID);
        passwordInput.setText(Constants.PASSWORD_VALUE);

        // get submit button
        HtmlSubmitInput submitButton = (HtmlSubmitInput) page
            .getElementById(Constants.SUBMIT_BUTTON_ID);

        HtmlPage afterLoginPage = submitButton.click();

       Set<Cookie> cookies = wc.getCookieManager().getCookies();
       ....
       ....
       // for every thread I create new WebClient
       for(threads loop){
           WebClient wc2 = initWebClient(cookies);
       }
    }

    private static WebClient initWebClient(Set<Cookie> cookies) {
       WebClient wc = new WebClient(BrowserVersion.CHROME);
       wc.getCookieManager().setCookiesEnabled(true);
       wc.getOptions().setJavaScriptEnabled(false);
       wc.getOptions().setThrowExceptionOnScriptError(false);

       if (cookies != null) {
          Iterator<Cookie> i = cookies.iterator();
          while (i.hasNext()) {
              wc.getCookieManager().addCookie(i.next());
          }
        }

        return wc;
   }

关于Java + HtmlUnit WebClient + SSL 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34314202/

相关文章:

java - 适用于 Android 的最佳 json rpc 2.0 库

java - 检测到设备正在关闭?

java - Windows 和 Linux 之间 Java 套接字的差异 - 如何处理它们?

java - 开源电子表格解决方案

android - SSL证书安卓

vb.net - 您可以在网络客户端更改用户代理吗?

c# - 从 MVC3 Action 中获取值(value)

php - 在 Ubuntu 10.04 上使用最新的 OpenSSL Opdate Curl

ssl - 客户端和服务器是否使用相同的证书文件通过 SSL 进行安全连接?

c# - 使用 WebClient C# 添加请求 header