jmeter 如何在JSR 223采样器中禁用HTTP请求的URL编码

hxzsmxv2  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(215)

我尝试使用JSR223 Sampler和以下groovy脚本自动执行应用程序的上载操作,但是该URI路径中的?被编码,我得到了http-404错误。
代码块:

import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
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.message.BasicHeader;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
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.ssl.SSLContextBuilder;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

def storageProtocol = "https";
def storageServer = "localhost";
def namespace = "7c762733-009f-4527-81ea-571d1cf6e9d2"
def studyUid = "1.2.300.0.7230010.3.1.2.2595064201.8112.1216202112026121";
def imageUid = "1.2.840.113704.7.1.0.1356918323635126.1521373008.110"

def RequestConfig uploadRequestConfig = RequestConfig.custom()
            .setConnectTimeout(30000)
            .setSocketTimeout(30000)
            .build();
def uploadUriBuilder = new URIBuilder().setScheme(storageProtocol)
            .setHost(storageServer);

def URI uri = uploadUriBuilder.setPath("/api/v3/storage/namespace/" + namespace + "/image?sid="+"${SIDVALUE}")
            .setParameter("study_uid", studyUid)
            .setParameter("image_uid", imageUid)
            .build();

def  RequestBuilder requestBuilder = RequestBuilder.create("POST")
        .setConfig(uploadRequestConfig)
        .setUri(uri)
        .setHeader("X-AmbraHealth-SID", "${SIDVALUE}")
        .setHeader("X-Requested-With", "XMLHttpRequest")
        .setHeader("X-DicomGrid-Client", "USER")
        .setHeader("X-DicomGrid-Version", "3.22.3.0")
        .setHeader("X-File-Name", "IMG00001.dcm")
        .setHeader("Referer", "https://${BASE_URL_1}/load.html?namespace_id=7c762733-009f-4527-81ea-571d1cf6e9d2")
        .setHeader(HttpHeaders.CONTENT_TYPE, "application/dicom");

def InputStreamEntity entity = new InputStreamEntity(new FileInputStream("C://Users//IMAGES//IMG00001.dcm"));
requestBuilder.setEntity(entity);

def sslBuilder = new SSLContextBuilder();
sslBuilder.loadTrustMaterial(null, new TrustStrategy(){
    @Override
    public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        return true;
        }
    });

def trustAllFactory = new SSLConnectionSocketFactory(sslBuilder.build(), new NoopHostnameVerifier());

HttpClients.custom().setSSLSocketFactory(trustAllFactory).build().withCloseable { httpClient ->
        httpClient.execute(requestBuilder.build()).withCloseable { response -> {
                def resp = EntityUtils.toString(response.getEntity());
            }
        }
    }

从脚本生成的请求URL:

https://localhost/api/v3/storage/namespace/7c762733-009f-4527-81ea-571d1cf6e9d2/image%3Fsid=fdbfbf07-bf4b-4ec5-b3cb-6e4353163f14?study_uid=1.2.300.0.7230010.3.1.2.2595064201.8112.1216202112026121&image_uid=1.2.840.113704.7.1.0.1356918323635126.1521373008.110

有两个错误,/image%3Fsid应为/image?sid*-6e4353163f14?study_uid=应为*-6e4353163f14&study_uid=
工作URL:

https://localhost/api/v3/storage/namespace/7c762733-009f-4527-81ea-571d1cf6e9d2/image?sid=fdbfbf07-bf4b-4ec5-b3cb-6e4353163f14&study_uid=1.2.300.0.7230010.3.1.2.2595064201.8112.1216202112026121&image_uid=1.2.840.113704.7.1.0.1356918323635126.1521373008.110

错误详细信息:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 404 Not Found</title>
</head>
<body><h2>HTTP ERROR 404 Not Found</h2>
<table>
<tr><th>URI:</th><td>/api/v2/namespace/7c762733-009f-4527-81ea-571d1cf6e9d2/image%3Fsid=fdbfbf07-bf4b-4ec5-b3cb-6e4353163f14</td></tr>
<tr><th>STATUS:</th><td>404</td></tr>
<tr><th>MESSAGE:</th><td>Not Found</td></tr>
<tr><th>SERVLET:</th><td>webservice</td></tr>
</table>
<hr><a href="https://eclipse.org/jetty">Powered by Jetty:// 9.4.43.v20210629</a><hr/>

</body>
</html>

如何处理此编码?

izj3ouym

izj3ouym1#

不是JSR223采样器对URL路径进行“编码”,而是执行此操作。
我只是想知道为什么你对study_uidimage_uid使用URIBuilder.setParameter()函数,而不是对sid这样做,因为一旦你开始这样做,这个问题就会消失。
另外,不要将JMeter Functions or Variables内联到Groovy脚本中,也就是说,将${SIDVALUE}更改为vars.get('SIDVALUE'),否则只有第一个匹配项将被缓存,后续迭代将使用相同的SIDVALUE,我认为这不是您正在寻找的东西。
更多信息:

相关问题