【问题标题】:What is the default Content Encoding for jmeter HTTP Request sampler?jmeter HTTP 请求采样器的默认内容编码是什么?
【发布时间】:2020-02-07 07:00:05
【问题描述】:

我正在查看 HTTP 请求采样器中的内容编码字段。不要将此与 HTTP Content-Type 标头混淆。

默认情况下,内容编码字段中的值为空。空是什么意思? jmeter HTTPRequest 的默认内容编码是什么?是 ASCII 还是 ANSI 还是 UTF-8?

这个guide只提到它不是必填字段。

【问题讨论】:

  • 你希望用这个值做什么?我觉得没有效果

标签: jmeter content-encoding


【解决方案1】:

Dmitri's answer 指向与 查询字符串 编码相关的代码,但这导致我查看 PostWriter class 的代码,它创建了实际的 body请求 - 如果采样器不提供内容编码 - ISO-8859-1 用于对正文进行编码:

public static final String ENCODING = StandardCharsets.ISO_8859_1.name();

...

String contentEncoding = sampler.getContentEncoding();
if(contentEncoding == null || contentEncoding.length() == 0) {
    contentEncoding = ENCODING;
}

【讨论】:

    【解决方案2】:

    指出为什么我对上面的回复投了反对票(Dmitri T 的回答):

    我遇到了西班牙口音和字符的问题,我花了几个小时试图弄清楚,假设上面的答案:留空相当于 UTF-8。将其专门设置为 UTF-8 后,不再出现西班牙语字符问题。

    所以,如果您需要 UTF-8,请不要留空。

    【讨论】:

      【解决方案3】:

      根据JMeter 5.2

      1. 调查HTTPSamplerBase.java:1136

        // Check if the sampler has a specified content encoding
        if (JOrphanUtils.isBlank(lContentEncoding)) {
            // We use the encoding which should be used according to the HTTP spec, which is UTF-8
            lContentEncoding = EncoderCache.URL_ARGUMENT_ENCODING;
        }
        
      2. 调查EncoderCache:31

        /** The encoding which should be usd for URLs, according to HTTP specification */
        public static final String URL_ARGUMENT_ENCODING = StandardCharsets.UTF_8.name();
        

      所以将该字段留空等于将其设置为UTF-8

      【讨论】:

      • 我认为这是不对的。在尝试将 JSON 发布到 API 时,我可以看到如果我将内容编码字段留空,则请求将失败。但是如果我将它设置为 UTF-8,那么它就可以工作了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-03
      • 2021-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多