简体   繁体   English

C ++ | Winsock - 编写HTTP Post以上传文件的正确方法?

[英]C++ | Winsock - Correct way to write HTTP Post for uploading a file?

Alert I've seen some people have asked but its usually about CURL, I am asking about sending it via WinSock2. 警报我见过一些人已经问过但它通常是关于CURL的,我问的是通过WinSock2发送它。

Alright so I know how to make POST and GET statements pretty easily, (POST with Application/application/x-www-form-urlencoded) but I am not sure how to use multipart/form-data, I know it has something to do with boundary to specify when you are done sending data. 好吧所以我知道如何轻松地发布POST和GET语句,(使用Application / application / x-www-form-urlencoded进行POST)但是我不知道如何使用multipart / form-data,我知道它有事情要做带边界指定何时完成发送数据。

But what should a basic HTTP POST look like for uploading files? 但是基本的HTTP POST应该如何才能上传文件呢? (Also can you upload to a PHP POSTBACK? If It was application/x-www-form-urlencoded I could see it something like filename=hello.png&data=...) (你也可以上传到PHP POSTBACK吗?如果它是application / x-www-form-urlencoded,我可以看到像filename = hello.png&data = ...)

Something like this format: (This is all I know about sending the data) I can't find a solid example online so I am asking the question. 像这样的格式:(这就是我所知道的关于发送数据的全部内容)我在网上找不到一个可靠的例子,所以我在问这个问题。

POST /postback.php HTTP/1.1
HOST: www.website.com
Content-Type: multipart/form-data; boundary=----IAmABoundary
Content-Length: 300

------IAmABoundary
Send Binary Data?
------IAmABoundary--

Use fiddler to capture uploading a file then you can see the actual format, your example above is more or less right as I recall. 使用fiddler来捕获上传文件然后你可以看到实际的格式,我记得上面的例子或多或少都是正确的。 Here is a much more complete answer with a good example: Example of multipart/form-data 这是一个更完整的答案,有一个很好的例子: multipart / form-data的例子

HTTP messages are MIME messages. HTTP消息是MIME消息。 It's actually a fairly complex encoding format. 它实际上是一种相当复杂的编码格式。

Start by reading RFC 2045 . 首先阅读RFC 2045 This is the base specification for MIME. 这是MIME的基本规范。

Then, move on to RFC 2046 . 然后,转到RFC 2046 Section 5 of RFC 2046 lays out the specification for multipart MIME messages, which is used for the multipart/form-data format you're seeing. RFC 2046的第5节列出了multipart MIME消息的规范,它用于您看到的multipart/form-data格式。

It's a lot of material to read through, true. 这是很多材料可以读,真的。 But it is imperative to fully understand MIME-formatted messages in order to have any hope of understand how file uploads work. 但是,必须完全理解MIME格式的消息,以便有理解文件上传的工作方式。

if you want to send file content actually you can just put the file content into after the HTTP Headers, and set the content-type and the content-length 如果你想实际发送文件内容,你可以把文件内容放在HTTP标题之后,并设置内容类型和内容长度

multipart/form-data is used by html form to post/submit form that have file in its field, they separated the parts/fields using the multipart boundary multipart / form-data由html表单用于发布/提交在其字段中包含文件的表单,它们使用多部分边界分隔部分/字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM