You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gatling Scala POST请求报j.i.IOException: Premature close错误求助

问题:Gatling发送x-www-form-urlencoded POST请求出现Premature close错误

我尝试用Gatling调用某URL发送POST请求提交表单数据,请求体类型为x-www-form-urlencoded,代码片段如下:

val sentHeadersSim = Map(
    "Accept" -> "*/*",
    "Content-Type" -> "application/x-www-form-urlencoded; charset=ISO-8859-1"
  )

 .exec(http("simulator POST call")
        .post("http://example.com:8000/sim")
        .headers(sentHeadersSim)
        .formParam("param1", "testdata1")
        .formParam("param2", "testdata2")
        .formParam("param3", "testdata3")
        .formParam("param4", "testdata4")
        .formParam("param5", "testdata5")
        .check(status.is(200))

运行时抛出j.i.IOException: Premature close错误,服务器在返回响应前关闭了连接。该请求在Postman中可正常返回200状态码,但Gatling日志显示请求可能未携带正确的表单体,发送后未收到服务器响应。日志详情如下:

HTTP request:
POST http://example.com:8000/sim
headers:
    Accept: */*
    Content-Type: application/x-www-form-urlencoded
    host: example.com:8000
    content-length: 73

body:FormUrlEncodedRequestBody{patchedContentType='null', charset=UTF-8, content=param1=testdata1&param2=testdata2&param3=testdata3&param4=testdata4&param5=testdata5
}

请问我是否遗漏了什么配置?


解决方案

从日志能看出两个核心问题:

  1. 你自定义的Content-Type头里指定的charset=ISO-8859-1未生效,实际请求头的Content-Type缺失了charset参数
  2. 表单数据实际编码为UTF-8,和你预期的ISO-8859-1不一致

这是因为Gatling使用.formParam()方法时,会自动生成Content-Type: application/x-www-form-urlencoded头,覆盖你自定义的带charset的头,同时默认用UTF-8编码表单数据。服务器可能对编码格式有严格要求,导致请求不符合预期,提前关闭连接。

你可以通过以下两种方式修复:

方式一:显式指定表单编码,让Gatling自动生成正确的Content-Type头

去掉自定义的Content-Type头,用.asFormUrlEncoded()指定编码:

import java.nio.charset.StandardCharsets

val sentHeadersSim = Map(
    "Accept" -> "*/*"
  )

.exec(http("simulator POST call")
        .post("http://example.com:8000/sim")
        .headers(sentHeadersSim)
        .formParam("param1", "testdata1")
        .formParam("param2", "testdata2")
        .formParam("param3", "testdata3")
        .formParam("param4", "testdata4")
        .formParam("param5", "testdata5")
        .asFormUrlEncoded(StandardCharsets.ISO_8859_1)
        .check(status.is(200))

方式二:手动构造表单数据并保留自定义头

如果必须手动控制Content-Type头,可以手动拼接表单字符串,指定编码后作为请求体:

import java.nio.charset.StandardCharsets
import java.net.URLEncoder

val formData = List(
  "param1" -> "testdata1",
  "param2" -> "testdata2",
  "param3" -> "testdata3",
  "param4" -> "testdata4",
  "param5" -> "testdata5"
).map { case (k, v) =>
  URLEncoder.encode(k, StandardCharsets.ISO_8859_1.name()) + "=" + URLEncoder.encode(v, StandardCharsets.ISO_8859_1.name())
}.mkString("&")

val sentHeadersSim = Map(
    "Accept" -> "*/*",
    "Content-Type" -> "application/x-www-form-urlencoded; charset=ISO-8859-1"
  )

.exec(http("simulator POST call")
        .post("http://example.com:8000/sim")
        .headers(sentHeadersSim)
        .body(StringBody(formData))
        .check(status.is(200))

另外,建议对比Postman的请求详情(查看Headers和Body的编码),确保Gatling的请求和Postman完全一致,比如是否有其他隐式请求头(如User-Agent)被Postman自动添加,而Gatling没有设置,这也可能导致服务器拒绝请求。


内容的提问来源于stack exchange,提问作者Sub9

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 12:53:25