gpt4 book ai didi

c++ - 使用 C++ 代码发送 POST 多部分/表单数据请求

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:55:02 24 4
gpt4 key购买 nike

我正在尝试编写一个客户端,它将连接到服务器并发出 HTTP POST 多部分/表单数据请求。请通过以下代码。使用此代码,我没有收到任何错误响应。我能够得到一些回应,但不完全是我所期望的。

使用此代码我可以访问 URL,但我认为服务器未读取参数(有效负载)。

我不确定我是否传递了适当的负载。

请帮我写一个合适的负载。谢谢。

void BuildHTTPRequest::BuildHeader1(stringstream &hdr,string payload)
{
hdr << "POST /home/one/user/send_event HTTP/1.1\r\n";
hdr << "Host:host.domain.com\r\n";
hdr << "Port:80\r\n";
hdr << "Content-Type:multipart/form-data; boundary=463762444806\r\n";
hdr << "Content-Length:" << payload.size() << "\r\n";
hdr << "\r\n";
hdr << payload;
}

void BuildHTTPRequest::BuildPayload1(string *payload)
{
(*payload).append("Content-Disposition: form-data; name=\"interface_version\"\r\n\r\n");
(*payload).append("01.0.01\r\n");
(*payload).append("-----------------------------463762444806\r\n");
(*payload).append("Content-Disposition: form-data; name=\"terminal_id\"\r\n\r\n");
(*payload).append("1\r\n");
(*payload).append("-----------------------------463762444806\r\n");
(*payload).append("Content-Disposition: form-data; name=\"device\"\r\n\r\n");
(*payload).append("display\r\n");
(*payload).append("-----------------------------463762444806\r\n");
(*payload).append("Content-Disposition: form-data; name=\"start_session\"\r\n\r\n");
(*payload).append("true\r\n");
(*payload).append("-----------------------------463762444806\r\n");
(*payload).append("Content-Disposition: form-data; name=\"first_contact\"\r\n\r\n");
(*payload).append("true\r\n");
(*payload).append("-----------------------------463762444806--\r\n\r\n");
}

最佳答案

您还应该在第一个有效负载数据之前有一个边界定界符,并且由于您没有在多部分数据之前放置任何前导码,因此您的请求 header 和有效负载之间需要 2 个 CRLF。

The requirement that the encapsulation boundary begins with a CRLF implies that the body of a multipart entity must itself begin with a CRLF before the first encapsulation line -- that is, if the "preamble" area is not used, the entity headers must be followed by TWO CRLFs. This is indeed how such entities should be composed.

此外,为什么在分隔符的边界值之前使用超过 2 个连字符?

规范说:

The Content-Type field for multipart entities requires one parameter, "boundary", which is used to specify the encapsulation boundary. The encapsulation boundary is defined as a line consisting entirely of two hyphen characters ("-", decimal code 45) followed by the boundary parameter value from the Content-Type header field.

...

Thus, a typical multipart Content-Type header field might look like this:

Content-Type: multipart/mixed; boundary=gc0p4Jq0M2Yt08jU534c0p   

This indicates that the entity consists of several parts, each itself with a structure that is syntactically identical to an RFC 822 message, except that the header area might be completely empty, and that the parts are each preceded by the line

 --gc0p4Jq0M2Yt08jU534c0p

试试这个:

void BuildHTTPRequest::BuildPayload1(string *payload)
{
(*payload).append("\r\n--463762444806\r\n");
(*payload).append("Content-Disposition: form-data; name=\"interface_version\"\r\n\r\n");
(*payload).append("01.0.01\r\n");
(*payload).append("--463762444806\r\n");
(*payload).append("Content-Disposition: form-data; name=\"terminal_id\"\r\n\r\n");
(*payload).append("1\r\n");
(*payload).append("--463762444806\r\n");
(*payload).append("Content-Disposition: form-data; name=\"device\"\r\n\r\n");
(*payload).append("display\r\n");
(*payload).append("--463762444806\r\n");
(*payload).append("Content-Disposition: form-data; name=\"start_session\"\r\n\r\n");
(*payload).append("true\r\n");
(*payload).append("--463762444806\r\n");
(*payload).append("Content-Disposition: form-data; name=\"first_contact\"\r\n\r\n");
(*payload).append("true\r\n");
(*payload).append("--463762444806--\r\n\r\n");
}

关于c++ - 使用 C++ 代码发送 POST 多部分/表单数据请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21017510/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com