PHP 自己设置 header

PHP sets header by itself

我在 php 上有一个脚本,它有一些输出,我在那里设置了 headers:

header("Content-Length: 20".$eol);
header("MIME-Version: 1.0".$eol);
header("Connection: Keep-Alive".$eol);
header("Accept-Encoding: gzip, deflate".$eol);
header("Host: host".$eol.$eol);
header("Content-Type: multipart/related; boundary=boundary_.oOo._MzA3NjM=NTczNw==NDM1Ng==".$eol);
echo $res;

我设置了 content-length,但是当我检查我的脚本时,页面将内容长度设置为 $res 的 strlen。有什么问题吗?

EDIT 据我了解代码是正确的。问题可能出在 Web 服务器中。 (我正在使用 Apache)
编辑 $eol="\r\n".
编辑

$res="--".$BOUNDARY.$eol;
$res .= "Content-Type: application/json".$eol;
$res .= "Content-Length: ".strlen("JSON").$eol.$eol;
$res .= "JSON";
$res.=$eol.$eol;
$res.="--".$BOUNDARY.$eol;
$res.="Content-Type: image/jpeg".$eol;
$res.="Content-Length: ".strlen("Image").$eol;
$res.=$eol;
$res.="Image";
$res.=$eol.$eol;
$res.="--".$BOUNDARY.$eol;
$res .= "Content-Type: application/octet-stream".$eol;
$res .= "Content-Length: ".strlen("Vector").$eol;
$res.=$eol;
$res.="Vector";
$res .= $eol.$eol;
$res .= "--".$BOUNDARY."--".$eol;

Body 的响应应使用 MIME。 Java 检查它的编码员要求我设置 content-length 即每个实体的 content-length 之和。

我认为您需要展示更多的代码。 $eol 包含什么,第一部分是 echo 的一部分吗?如果 html 的格式不正确,事情就会变得杂乱无章。我认为这是你的问题。在浏览器上尝试 html 测试站点或插件。

The HTTP protocol follows the robustness principle as described in RFC1122, which states "Be liberal in what you accept, and conservative in what you send". As a result of this principle, HTTP clients will compensate for and recover from incorrect or misconfigured responses, or responses that are uncacheable.

Source

因为您发送了错误的 Content-Length 值,Apache 已为您更正。