从 TIdMultiPartFormDataStream 中剥离 "content-type"

striping out the "content-type" from TIdMultiPartFormDataStream

这与 post 有点相关。我正在尝试使用 TIdHTTPTIdMultiPartFormDataStream post 一些表单数据,但是当使用 Wireshark[=29 监视通信时=],每个表单字段都有一个 content-Type: text/plain 附加到它,但出于某种原因,我发送这些东西的服务器不喜欢它。有没有一种方法可以确保只发送名称和值? Content-Transfer 也被添加了,我可以使用以下方法删除它:

aFieldItem := PostStream.AddFormField(fName, fValue);
aFieldItem.ContentTransfer := '';

但我找不到任何方法来摆脱内容类型。 此时正在发送的数据看起来像这样(在 Wireshark 中)

Boundary: \r\n----------051715151353026\r\n
Encapsulated multipart part:  (text/plain)
    Content-Disposition: form-data; name="description"\r\n
    Content-Type: text/plain\r\n
    Line-based text data: text/plain
        \r\n
        Testing new AW Mobile

我希望它看起来像:

Boundary: \r\n------WebKitFormBoundary32hCBG8zkGMBpxqL\r\n
Encapsulated multipart part:
    Content-Disposition: form-data; name="description"\r\n
    Data (21 bytes)
        Data: 0d0a5465737420616e6420747261636520636f6d6d
        Length: 21

谢谢 山姆

HTML5 Section 4.10.22.7 alters how RFC 2388 适用于网络表单提交:

The parts of the generated multipart/form-data resource that correspond to non-file fields must not have a Content-Type header specified. Their names and values must be encoded using the character encoding selected above (field names in particular do not get converted to a 7-bit safe encoding as suggested in RFC 2388).

这与 RFC 2388 不同:

As with all multipart MIME types, each part has an optional "Content-Type", which defaults to text/plain.

您的服务器显然期待 HTML5 行为。

添加到 TIdMultipartFormDataStream 的每个 MIME 部分的 Content-Type header 是 hard-coded 并且在不更改 TIdMultipartFormDataStream 的情况下无法删除'通过将 TIdFormDataField.ContentType 属性 设置为 space 字符(不是空字符串,如 ContentTransfer 属性 可以省略 s source code允许):

aFieldItem := PostStream.AddFormField(fName, fValue);
aFieldItem.ContentTransfer := '';
aFieldItem.ContentType := ' '; // <-- here

如果您将 ContentType 属性 设置为空字符串,它会将 Content-Type header 设置为 application/octet-stream,但分配一个 [=当 属性 setter 解析新值时,44=] 字符会产生忽略 header 的副作用。

也就是说,我已经对 TIdMultipartFormDataStream 进行了一些更改,以解决 HTML5 中网络表单提交的这一变化,但我还没有完成并发布它。