问题描述
我有一个关于使用Apach JMeter的问题.
I have a question for using Apach JMeter.
我们的项目Android应用将带有"Gzip压缩"的json数据发布到API服务器.使用"Apache HttpClient"及其"GzipCompressingEntity"类的Android应用.
Our project, Android apps post json data with "Gzip Compression" to API server.The Android apps using "Apache HttpClient" and it's "GzipCompressingEntity" class.
对于API服务器的性能测试,我尝试通过JMeter的代理(="HTTP(S)Test Script Recorder")记录请求.但是记录的请求正文为空.
For performance testing of API server, I tryed to recording the request by JMeter's Proxy (="HTTP(S) Test Script Recorder").But the recorded request body was empty.
我想做的是从Apache JMeter向服务器发送"Gzip压缩的HTTP 请求数据".有什么办法吗?
What I want to do is to send "Gziped HTTP request data" from Apache JMeter to server.Is there any way for that?
以下是我们的Android应用的请求标头示例.
Following is sample of our Android app's request header.
POST /api/test HTTP/1.1
Content-Type: application/json
Transfer-Encoding: chunked
Content-Encoding: gzip
Host: 192.168.11.11:8080
Connection: Keep-Alive
User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.5)
Accept-Encoding: gzip,deflate
RequestBody是压缩的二进制数据.
我所做的是
- 在JMeter上运行"HTTP(S)测试脚本记录器(代理服务器)".
- 设置Android的http代理以使用该代理服务器.
- 执行HTTP客户端的Android测试代码(发布数据由Gzip压缩).
然后,该测试代码以失败告终. (服务器未响应)
then, that test code finished with failure. (no response from server)
如果直接访问(没有JMeter的代理服务器),则测试成功.我可以像这样从JMeter发送压缩的请求数据吗?
If access directly (without JMeter's Proxy Server), that test succeeded.Can I send the compressed request data from JMeter just like this?
推荐答案
-
将 HTTP标头管理器添加到您的测试计划中,至少添加以下标头:
Add HTTP Header Manager to your test plan and add at least the following headers:
- 内容类型:application/json
- 内容编码:gzip
将 Beanshell预处理器添加为您所请求的子项需要对以下代码进行编码并将其添加到脚本"区域:
Add Beanshell PreProcessor as a child of the request which you need to encode and add the following code to it's "Script" area:
import org.apache.commons.io.IOUtils;
import java.util.zip.GZIPOutputStream;
String bodyString = sampler.getArguments().getArgument(0).getValue();
byte [] requestBody = bodyString.getBytes();
ByteArrayOutputStream out = new ByteArrayOutputStream(requestBody.length);
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(requestBody);
gzip.close();
sampler.getArguments().getArgument(0).setValue(out.toString(0));
它将获取您的请求正文,对其进行压缩并即时替换,以便将请求压缩.
It will get your request body, compress it and substitute on the fly so the request will be gzipped.
这篇关于如何使用Apache JMeter发布GZip请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!