【问题标题】:How to send a gzip file as byte array in HTTP request in JMeter如何在 JMeter 的 HTTP 请求中将 gzip 文件作为字节数组发送
【发布时间】:2014-05-11 14:04:37
【问题描述】:

我有一个发布请求,我必须将 gzip 文件作为字节数组发送。 我尝试了以下方法:

  1. 创建了一个 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. 在参数选项卡下的 HTTP 请求采样器中添加参数 postData 作为 ${postData} 。还尝试在 BodyData 选项卡下的 HTTP Request Sampler 中添加参数 ${postData}。

但是文件没有发送。

  1. 我还尝试通过将文件添加到 HTTP 请求采样器的“使用请求发送文件”选项卡下并使用适当的编码来发送文件。

但对于所有情况,我都会收到错误消息:

:{"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) 

【问题讨论】:

    标签: arrays jmeter beanshell


    【解决方案1】:

    首先,我不喜欢你的文件路径C:\Users\New\Desktop\Load_Testing。在 Java 和 Beanshell 中,您需要转义像 C:\\Users\\New\\Desktop\\Load_Testing 这样的反斜杠

    第二:确保您发送的gzip 文件正确无误。如果您的 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();
    

    最后,确保您发送正确的 Accept-Encoding 标头。为此,请添加 HTTP Header Manager 并在 Accept-Encoding 节中包含 gzip 值。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2014-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-21
      相关资源
      最近更新 更多