简体   繁体   English

如何在JMeter的HTTP请求中将gzip文件作为字节数组发送

[英]How to send a gzip file as byte array in HTTP request in JMeter

I have a post request where I have to send a gzip file as byte array.我有一个 post 请求,我必须将 gzip 文件作为字节数组发送。 I have tried the following:我尝试了以下方法:

  1. Created a BeanShell preprocessor to convert the file to byte array with the following function(got this function from here) :创建了一个 BeanShell 预处理器,使用以下函数将文件转换为字节数组(从此处获得此函数):
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream; 

FileInputStream in = new FileInputStream("C:\Users\New\Desktop\Load_Testing");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
for (int i; (i = in.read(buffer)) != -1; ) {
    bos.write(buffer, 0, i);
}
in.close();
byte[] postData = bos.toByteArray();
bos.close();
vars.put("postData", new String(postData));
  1. Added the parameter postData in HTTP Request Sampler under Parameter's tab as ${postData} .在参数选项卡下的 HTTP 请求采样器中添加参数 postData 作为 ${postData} 。 Also tried adding the parameter ${postData} in HTTP Request Sampler under BodyData tab.还尝试在 BodyData 选项卡下的 HTTP 请求采样器中添加参数 ${postData}。

But the file is not sent.但是文件没有发送。

  1. I also tried sending the file by adding it under the tab "Send Files With the Request" section in HTTP request sampler with appropriate encoding.我还尝试通过将文件添加到 HTTP 请求采样器中的选项卡“随请求发送文件”部分并使用适当的编码来发送文件。

But for all the cases I am getting the error :但对于所有情况,我都收到错误消息:

:{"Result":"Error uploading data stream
The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.
   at System.IO.Compression.GZipDecoder.ReadHeader(InputBuffer input)
   at System.IO.Compression.Inflater.Decode()
   at System.IO.Compression.Inflater.Inflate(Byte[] bytes, Int32 offset, Int32 length)
   at System.IO.Compression.DeflateStream.Read(Byte[] array, Int32 offset, Int32 count)
   at System.IO.Compression.GZipStream.Read(Byte[] array, Int32 offset, Int32 count)
   at System.IO.Stream.ReadByte()
   at ServiceData.CompressionHelper.UnGZip(Byte[] input) 

First of all, I don't like your file path C:\\Users\\New\\Desktop\\Load_Testing .首先,我不喜欢你的文件路径C:\\Users\\New\\Desktop\\Load_Testing In Java and Beanshell you need to escape back slashes like C:\\\\Users\\\\New\\\\Desktop\\\\Load_Testing在 Java 和 Beanshell 中,您需要转义反斜杠,如C:\\\\Users\\\\New\\\\Desktop\\\\Load_Testing

Second: make sure that you're sending a correct gzip file.第二:确保您发送的是正确的gzip文件。 If Your C:\\Users\\New\\Desktop\\Load_Testing file is not in gzip format, you'll need to convert compress it in your Beanshell Pre-Processor as follows:如果你的C:\\Users\\New\\Desktop\\Load_Testing文件不是 gzip 格式,你需要在你的 Beanshell 预处理器中转换压缩它,如下所示:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.GZIPOutputStream;

byte[] buffer = new byte[1024];
GZIPOutputStream gzos = new GZIPOutputStream(new FileOutputStream("C:/Users/New/Desktop/Load_Testing.gz"));
FileInputStream in = new FileInputStream("C:/Users/New/Desktop/Load_Testing");

int len;
while ((len = in.read(buffer)) > 0) {
    gzos.write(buffer, 0, len);
}

in.close();

gzos.finish();
gzos.close();

And finally, make sure that you're sending correct Accept-Encoding header.最后,确保您发送的是正确的 Accept-Encoding 标头。 To do so add a HTTP Header Manager and include gzip value in Accept-Encoding stanza.为此,请添加HTTP 标头管理器并在Accept-Encoding节中包含gzip值。

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM