如何防止 jmeter 请求跟随重定向
How to prevent jmeter request from following a redirect
HTTP 请求下有两个选项- 自动重定向 和跟随重定向。我想在未选中 Follow Redirects 时实施行为
//CASE 1: HttpClient client = HttpClientBuilder.create().build();
//CASE 2: HttpClient client = HttpClientBuilder.create().disableRedirectHandling().build();
HttpGet get = new HttpGet("${request_link}");
HttpResponse response = client.execute(get);
SampleResult.setResponseData(EntityUtils.toByteArray(response.getEntity()));
我正在使用上面案例 2 中所示的“disableRedirectHandling”来停止自动 re-direct。但我得到的是 200 响应代码,而不是预期的 302。
这是我对案例 1 得到的回复:
响应代码:200
响应信息:OK
响应 headers:
但我期待以下回复:
响应代码:302
响应消息:找到
回应headers:
HTTP/1.1 302 找到
日期:2021 年 6 月 11 日星期五 00:32:45 GMT
Content-Length: 0
连接:keep-alive
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
编译指示:no-cache
过期:0
位置:{重定向位置link}
注意:disableRedirectHandling() 正在禁用 自动重定向 但我想要实现的是 禁用跟随重定向
基本上我想要这个
setRedirectsEnabled(假)
或 setMaxRedirects(0)
我试过了,但运气不好,仍然得到 re-direct
HttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(RequestConfig.custom().setRedirectsEnabled(true).setMaxRedirects(0).build()).build();
您的案例 2 看起来有效,问题是否出在您的 request_link
变量上?
根据JSR223 Sampler documentation:
The JSR223 test elements have a feature (compilation) that can significantly increase performance. To benefit from this feature:
Use Script files instead of inlining them. This will make JMeter compile them if this feature is available on ScriptEngine and cache them.
Or Use Script Text and check Cache compiled script if available property.
When using this feature, ensure your script code does not use JMeter variables or JMeter function calls directly in script code as caching would only cache first replacement. Instead use script parameters.
因此,请尝试将 HttpGet("${request_link}");
替换为 HttpGet(vars.get("request_link"));
,其中 vars
代表 JMeterVariables class instance (see Top 8 JMeter Java Classes You Should Be Using with Groovy,以获取有关此快捷方式和其他 JMeter API 快捷方式的更多信息)
一般来说,有什么不使用 JMeter 的 HTTP Request sampler 来构建请求的具体原因吗?它支持 cookie、header、缓存管理器、嵌入式资源、身份验证、更好地与报告集成等。
HTTP 请求下有两个选项- 自动重定向 和跟随重定向。我想在未选中 Follow Redirects 时实施行为
//CASE 1: HttpClient client = HttpClientBuilder.create().build();
//CASE 2: HttpClient client = HttpClientBuilder.create().disableRedirectHandling().build();
HttpGet get = new HttpGet("${request_link}");
HttpResponse response = client.execute(get);
SampleResult.setResponseData(EntityUtils.toByteArray(response.getEntity()));
我正在使用上面案例 2 中所示的“disableRedirectHandling”来停止自动 re-direct。但我得到的是 200 响应代码,而不是预期的 302。
这是我对案例 1 得到的回复: 响应代码:200 响应信息:OK 响应 headers:
但我期待以下回复: 响应代码:302 响应消息:找到
回应headers: HTTP/1.1 302 找到 日期:2021 年 6 月 11 日星期五 00:32:45 GMT Content-Length: 0 连接:keep-alive Cache-Control: no-cache, no-store, max-age=0, must-revalidate 编译指示:no-cache 过期:0 位置:{重定向位置link}
注意:disableRedirectHandling() 正在禁用 自动重定向 但我想要实现的是 禁用跟随重定向
基本上我想要这个 setRedirectsEnabled(假) 或 setMaxRedirects(0)
我试过了,但运气不好,仍然得到 re-direct
HttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(RequestConfig.custom().setRedirectsEnabled(true).setMaxRedirects(0).build()).build();
您的案例 2 看起来有效,问题是否出在您的 request_link
变量上?
根据JSR223 Sampler documentation:
The JSR223 test elements have a feature (compilation) that can significantly increase performance. To benefit from this feature:
Use Script files instead of inlining them. This will make JMeter compile them if this feature is available on ScriptEngine and cache them.
Or Use Script Text and check Cache compiled script if available property. When using this feature, ensure your script code does not use JMeter variables or JMeter function calls directly in script code as caching would only cache first replacement. Instead use script parameters.
因此,请尝试将 HttpGet("${request_link}");
替换为 HttpGet(vars.get("request_link"));
,其中 vars
代表 JMeterVariables class instance (see Top 8 JMeter Java Classes You Should Be Using with Groovy,以获取有关此快捷方式和其他 JMeter API 快捷方式的更多信息)
一般来说,有什么不使用 JMeter 的 HTTP Request sampler 来构建请求的具体原因吗?它支持 cookie、header、缓存管理器、嵌入式资源、身份验证、更好地与报告集成等。