当前位置: 首页 > 知识库问答 >
问题:

JSR223采样器SSL证书问题

常波鸿
2023-03-14

我在JSR223采样器中有以下代码,我得到SSL证书错误。有什么办法可以做到禁用吗?

JSR223脚本中的问题JSR223采样器,消息: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));

共有1个答案

宋博易
2023-03-14

根据文档,只需使用普通的HTTP请求采样器即可:

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));

更多信息:

  • 通过https使用HttpClient信任所有证书
  • Apache Groovy-为什么和如何使用它
 类似资料:
  • 我有下面的JSR223采样器,它读取图像,稍微修改它,并发送一个POST multipart/form-data请求。与HTTP采样器相比,我发现它广泛使用了CPU,但我不能使用HTTP采样器,因为它不支持在不保存到文件系统的情况下更改映像。 如果任何人有任何输入来优化JSR223采样器中的脚本,这样它就不会占用大量的CPU,我将很感激。

  • 我是Jmeter的新手,正在积极学习。 我想知道JSR223采样器和JSR223预处理器有什么不同,采样器可以和预处理器一样使用吗?

  • 我已经在中获得了记录数,我只需要知道如何才能获得的数据 提前感谢!

  • 我正在为TIBCO EMS队列测试运行一个JMS点对点采样器。我已经将所有tibco ems JAR添加到jmeter lib文件夹中。在JMS采样器中配置队列详细信息、上下文工厂、用户凭据。当使用tcp请求队列击中TIBCO ems时,它正在工作。但是当用SSL url击中时,它无法连接。 我尝试了以下方式来附上tibco团队给出的自签名证书

  • 我试图使用JSR223采样器模拟并行ajax请求,如这里提到的https://www.blazemeter.com/blog/how-load-test-ajaxxhr-enabled-sites-jmeter ,我在测试计划中启用了cookie管理器。 任何关于我做错什么的帮助都是很好的。