在C ++中通过HTTP POST上传文件

时间:2022-08-21 23:55:15

I'm trying to send a file and other POST variables to a PHP script on my server. There are no good resources on Google and the code samples I've found don't work. Preferably without using cURL.

我正在尝试将文件和其他POST变量发送到我的服务器上的PHP脚本。谷歌没有很好的资源,我发现的代码示例不起作用。优选不使用cURL。

5 个解决方案

#1


If you're going to roll your own you'd need the relevant RFC for HTTP file uploading (googling on "rfc http file upload" will yield the same result). This RFC also shows how to handle a mix of files and other FORM-data (or POST variables). The problem is of course that you'll probably want to read the MIME RFC as well...

如果您打算自己动手,则需要相关的RFC上传HTTP文件(在“rfc http文件上传”上搜索将产生相同的结果)。此RFC还显示了如何处理混合的文件和其他FORM数据(或POST变量)。问题当然是您可能也想要阅读MIME RFC ...

#2


Just a couple of resources make it pretty easy to roll your own

只需几个资源就可以轻松推出自己的资源

Here is an example of a GET request via ASIO (the C++ networking library in Boost)

以下是通过ASIO(Boost中的C ++网络库)发出GET请求的示例

Here is the HTTP protocol made really easy

这里的HTTP协议非常简单

The GET request is how you can view any page on your site. With that code you can download any page and get it as raw text. As you can see it sends a GET header to the server. As explained in that HTTP protocol page, the POST request looks like this

GET请求是您查看网站上任何页面的方式。使用该代码,您可以下载任何页面并将其作为原始文本。如您所见,它将GET标头发送到服务器。正如HTTP协议页面中所解释的那样,POST请求如下所示

POST /path/script.cgi HTTP/1.0 From:
frog@jmarshall.com User-Agent:
HTTPTool/1.0 Content-Type:
application/x-www-form-urlencoded
Content-Length: 32

home=Cosby&favorite+flavor=flies
  1. To send a file:
  2. 要发送文件:

  3. You put your URL after post
  4. 您在发布后放置了您的URL

  5. change the content type to the type of file you are trying to upload.
  6. 将内容类型更改为您尝试上传的文件类型。

  7. Set Content-Length to the number of bytes in that file
  8. 将Content-Length设置为该文件中的字节数

  9. Append the file after a carrage return (replace "home=Cosby&favorite+flavor=flies")
  10. 在carrage返回后附加文件(替换“home = Cosby&favorite + flavor = flies”)

#3


Another (more quick-n-dirty) solution is to use a utility, via a system() or similar call. For example the wget utility has a --post-file option.

另一种(更快速的肮脏)解决方案是使用实用程序,通过系统()或类似的调用。例如,wget实用程序有一个--post-file选项。

#4


I'd say roll your own. Its not too complicated.

我会说你自己滚。它不太复杂。

Capture an HTTP post sent from a browser in Wireshark and reverse engineer as necessary using the spec as your guide. (See Andreas Magnusson's answer below for perhaps more relevant specs.)

捕获从Wireshark中的浏览器发送的HTTP帖子,并根据需要使用规范作为指南进行反向工程。 (有关更多相关规范,请参阅下面的Andreas Magnusson的答案。)

I would recommend this approach personally for learning the protocol rather than just going pure spec. Its pretty difficult to learn things just from the spec. I would rather explore the different behaviors by known http clients and try to figure out how things are working by using the spec as my guide.

我会亲自推荐这种方法来学习协议,而不仅仅是纯粹的规范。从规范中学习东西很难。我宁愿通过已知的http客户端探索不同的行为,并尝试使用规范作为我的指南来弄清楚事情是如何工作的。

Format and send the data accordingly over a socket once you're comfortable with HTTP.

一旦您对HTTP感到满意,请通过套接字格式化并发送数据。

Also, If you are not familiar with socket programming, check out Beej's guide to socket programming.

另外,如果您不熟悉套接字编程,请查看Beej的套接字编程指南。

#5


this worked great for me on debian (http get, http post):

这对我来说非常适合debian(http get,http post):

http://cpp-netlib.github.com

I use v 0.9.3 that requires boost 1.49

我使用v 0.9.3,需要提升1.49

#1


If you're going to roll your own you'd need the relevant RFC for HTTP file uploading (googling on "rfc http file upload" will yield the same result). This RFC also shows how to handle a mix of files and other FORM-data (or POST variables). The problem is of course that you'll probably want to read the MIME RFC as well...

如果您打算自己动手,则需要相关的RFC上传HTTP文件(在“rfc http文件上传”上搜索将产生相同的结果)。此RFC还显示了如何处理混合的文件和其他FORM数据(或POST变量)。问题当然是您可能也想要阅读MIME RFC ...

#2


Just a couple of resources make it pretty easy to roll your own

只需几个资源就可以轻松推出自己的资源

Here is an example of a GET request via ASIO (the C++ networking library in Boost)

以下是通过ASIO(Boost中的C ++网络库)发出GET请求的示例

Here is the HTTP protocol made really easy

这里的HTTP协议非常简单

The GET request is how you can view any page on your site. With that code you can download any page and get it as raw text. As you can see it sends a GET header to the server. As explained in that HTTP protocol page, the POST request looks like this

GET请求是您查看网站上任何页面的方式。使用该代码,您可以下载任何页面并将其作为原始文本。如您所见,它将GET标头发送到服务器。正如HTTP协议页面中所解释的那样,POST请求如下所示

POST /path/script.cgi HTTP/1.0 From:
frog@jmarshall.com User-Agent:
HTTPTool/1.0 Content-Type:
application/x-www-form-urlencoded
Content-Length: 32

home=Cosby&favorite+flavor=flies
  1. To send a file:
  2. 要发送文件:

  3. You put your URL after post
  4. 您在发布后放置了您的URL

  5. change the content type to the type of file you are trying to upload.
  6. 将内容类型更改为您尝试上传的文件类型。

  7. Set Content-Length to the number of bytes in that file
  8. 将Content-Length设置为该文件中的字节数

  9. Append the file after a carrage return (replace "home=Cosby&favorite+flavor=flies")
  10. 在carrage返回后附加文件(替换“home = Cosby&favorite + flavor = flies”)

#3


Another (more quick-n-dirty) solution is to use a utility, via a system() or similar call. For example the wget utility has a --post-file option.

另一种(更快速的肮脏)解决方案是使用实用程序,通过系统()或类似的调用。例如,wget实用程序有一个--post-file选项。

#4


I'd say roll your own. Its not too complicated.

我会说你自己滚。它不太复杂。

Capture an HTTP post sent from a browser in Wireshark and reverse engineer as necessary using the spec as your guide. (See Andreas Magnusson's answer below for perhaps more relevant specs.)

捕获从Wireshark中的浏览器发送的HTTP帖子,并根据需要使用规范作为指南进行反向工程。 (有关更多相关规范,请参阅下面的Andreas Magnusson的答案。)

I would recommend this approach personally for learning the protocol rather than just going pure spec. Its pretty difficult to learn things just from the spec. I would rather explore the different behaviors by known http clients and try to figure out how things are working by using the spec as my guide.

我会亲自推荐这种方法来学习协议,而不仅仅是纯粹的规范。从规范中学习东西很难。我宁愿通过已知的http客户端探索不同的行为,并尝试使用规范作为我的指南来弄清楚事情是如何工作的。

Format and send the data accordingly over a socket once you're comfortable with HTTP.

一旦您对HTTP感到满意,请通过套接字格式化并发送数据。

Also, If you are not familiar with socket programming, check out Beej's guide to socket programming.

另外,如果您不熟悉套接字编程,请查看Beej的套接字编程指南。

#5


this worked great for me on debian (http get, http post):

这对我来说非常适合debian(http get,http post):

http://cpp-netlib.github.com

I use v 0.9.3 that requires boost 1.49

我使用v 0.9.3,需要提升1.49