【问题标题】:JSR223 Sampler SSL certificate issuesJSR223 Sampler SSL 证书问题
【发布时间】:2020-09-28 12:59:17
【问题描述】:

我在 JSR223 Sampler 中有以下代码,但出现 SSL 证书错误。有什么办法可以禁用吗?

JSR223 脚本 JSR223 Sampler 中的问题,消息:javax.script.ScriptException: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX 路径构建失败

import org.apache.http.HttpHeaders;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.http.entity.StringEntity;


List<String> sendRequest(String url, String method, String body) {


    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(2000)
            .setSocketTimeout(3000)
            .build();

    StringEntity entity = new StringEntity(body, "UTF-8");


    HttpUriRequest request = RequestBuilder.create(method)
            .setConfig(requestConfig)
            .setUri(url)
            .setHeader(HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8")
            .setEntity(entity)
            .build();

   String req = "REQUEST:" + "\n" + request.getRequestLine() + "\n" + "Headers: " +
            request.getAllHeaders() + "\n" + EntityUtils.toString(entity) + "\n";


    HttpClientBuilder.create().build().withCloseable {httpClient ->

        httpClient.execute(request).withCloseable {response ->

            String res = "RESPONSE:" + "\n" + response.getStatusLine() + "\n" + "Headers: " +
                    response.getAllHeaders() + "\n" +
                    (response.getEntity() != null ? EntityUtils.toString(response.getEntity()) : "") + "\n";

            System.out.println(req + "\n"  + res );

            return Arrays.asList(req, res);
        }
    }
}

List test1 = sendRequest("https://testserver.com","POST", "");
log.info(Arrays.toString(test1));

【问题讨论】:

    标签: ssl jmeter jsr223


    【解决方案1】:

    根据文档,只需使用普通的 HTTP Request 采样器:

    JMeter HTTP 采样器配置为接受所有证书,无论是否受信任,无论有效期等。这是为了在测试服务器时提供最大的灵活性


    但是,如果您正在做一些非常特别的事情并且需要在 Groovy 中做同样的事情 - 这是示例解决方案:

    import org.apache.http.HttpHeaders
    import org.apache.http.client.config.RequestConfig
    import org.apache.http.client.methods.HttpUriRequest
    import org.apache.http.client.methods.RequestBuilder
    import org.apache.http.conn.ssl.NoopHostnameVerifier
    import org.apache.http.conn.ssl.SSLConnectionSocketFactory
    import org.apache.http.conn.ssl.TrustStrategy
    import org.apache.http.entity.StringEntity
    import org.apache.http.impl.client.HttpClients
    import org.apache.http.ssl.SSLContextBuilder
    import org.apache.http.util.EntityUtils
    
    import java.security.cert.CertificateException
    import java.security.cert.X509Certificate
    
    List<String> sendRequest(String url, String method, String body) {
    
    
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(2000)
                .setSocketTimeout(3000)
                .build();
    
        StringEntity entity = new StringEntity(body, "UTF-8");
    
    
        HttpUriRequest request = RequestBuilder.create(method)
                .setConfig(requestConfig)
                .setUri(url)
                .setHeader(HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8")
                .setEntity(entity)
                .build();
    
        String req = "REQUEST:" + "\n" + request.getRequestLine() + "\n" + "Headers: " +
                request.getAllHeaders() + "\n" + EntityUtils.toString(entity) + "\n";
    
        def builder = new SSLContextBuilder();
        builder.loadTrustMaterial(null, new TrustStrategy() {
            @Override
            public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                return true;
            }
        });
        def trustAllFactory = new SSLConnectionSocketFactory(builder.build(), new NoopHostnameVerifier());
    
        HttpClients.custom().setSSLSocketFactory(trustAllFactory).build().withCloseable { httpClient ->
    
            httpClient.execute(request).withCloseable { response ->
    
                String res = "RESPONSE:" + "\n" + response.getStatusLine() + "\n" + "Headers: " +
                        response.getAllHeaders() + "\n" +
                        (response.getEntity() != null ? EntityUtils.toString(response.getEntity()) : "") + "\n";
    
                System.out.println(req + "\n" + res);
    
                return Arrays.asList(req, res);
            }
        }
    }
    
    List test1 = sendRequest("https://testserver.com", "POST", "");
    println(Arrays.toString(test1));
    

    更多信息:

    【讨论】:

    • 这就像一个魅力。太感谢了。我需要使用 JSR223Sampler 发送 Multipart 表单数据,即时更改图像。我无法通过 HTTPSamplerProxy 实现这一目标
    猜你喜欢
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 2017-09-07
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多