在远程调试期间,忽略httpclient的SSL错误时,Webdriver会挂起

时间:2019-02-16 20:37:39

标签: selenium ssl selenium-webdriver apache-httpclient-4.x

您好,感谢您的宝贵时间:

背景

我有点“继承”了这个框架,并且在“背景”中提到的内容是一成不变的,甚至更糟,由于某些原因我无法更改它们。

  • 使用基于WebDriver的框架
  • 使用BrowserMobProxy来绕过特定网站的基本身份验证
  • 需要在运行一些测试用例期间或之前以编程方式发送http请求

问题

(仅在调试过程中!)使用无效的安全证书将HTTP Post请求发送到(特定的)URL后,Webdriver会初始化,但实际上访问任何网站的速度非常慢。有时甚至终止连接。

浏览器左下角显示的消息是建立安全连接(对于Chrome)和执行TLS握手(对于Firefox)

这仅在远程调试期间发生。

其他信息:

  1. 线程化(?)Webdriver框架
  2. 对于正在使用httpcomponents v4.4的http请求,我是在apache / google / stackoverflow的帮助下实现的
  3. 将httpClient设置为忽略SSL错误
  4. (!)当不忽略SSL错误时,http请求显然会失败,但是即使在调试过程中,webdriver也可以正常工作
  5. 通过将我的IDE连接到远程JVM的端口5005,使用maven故障安全调试执行调试

httpclient相关内容的实现

该代码正在运行,我只是对其一部分进行了裁剪以仅保留相关部分:

httpClient     SSLContext sslContext = getSSLContext();

private SSLContext getSSLContext() {
    try {
        return SSLContexts.custom()
                .loadTrustMaterial(null, new TrustSelfSignedStrategy())
                .build();
    } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
        throw new RuntimeException("Could not create custom SSLContext: ", e);
    }
}

SSLConnectionSocketFactory sslConnectionSocketFactory = getSslConnectionSocketFactory(sslContext);

HttpClientBuilder httpClientBuilder = HttpClientBuilder.create()
    .setSSLSocketFactory(sslConnectionSocketFactory)
    .setDefaultCredentialsProvider(getConfiguredCredentialsProvider())
    .build();

sendPost

public CustomHttpResponse sendPost(String url, HttpEntity httpEntity) {
    HttpPost httpPost = new HttpPost(url);

    int statusCode;
    String responseString;

    if (httpEntity != null) {
        httpPost.setEntity(httpEntity);
    }

    CloseableHttpResponse closeableHttpResponse = null;
    try {
        closeableHttpResponse = httpClient.execute(httpPost);
        HttpEntity responseEntity = closeableHttpResponse.getEntity();
        responseString = EntityUtils.toString(responseEntity);
        statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
    } catch (IOException exception) {
        throw new RuntimeException("Unable to close or execute a POST connection: ", exception);
    } finally {
        try {
            closeableHttpResponse.close();
        } catch (IOException e) {
          //exceptionHandling
        }
    }

    if (statusCode == 0 && responseString == null) {
        throw new RuntimeException("StatusCode or responseString is null!");
    }

    return new CustomHttpResponse(statusCode, responseString);
}

0 个答案:

没有答案