简体   繁体   English

使用C ++将文件上载到Web服务器

[英]Upload a file to a web server using C++

I want to upload files from a client location to a server. 我想将文件从客户端位置上传到服务器。 At present, I have a client-server socket program by which I could send/receive files across, but I would like to improvise it. 目前,我有一个客户端 - 服务器套接字程序,我可以通过它发送/接收文件,但我想即兴发挥它。

My idea would be to transfer the file using HTTP PUT/POST from client (most of the coding on client side) to the server. 我的想法是使用HTTP PUT / POST从客户端(客户端的大多数编码)传输文件到服务器。 Since I have no idea about HTTP programming, so I need some guidance on how to achieve that. 由于我不知道HTTP编程,所以我需要一些如何实现它的指导。 I want to use C++ with BSD sockets in doing that, and no other libraries. 我想在这样做时使用带有BSD套接字的C ++,而不是其他库。 My aim is to send the server a form, like as given below with a HTTP POST/PUT request, and get the file "main.cpp" uploaded to the server. 我的目标是向服务器发送一个表单,如下面给出的HTTP POST / PUT请求,并将文件“main.cpp”上传到服务器。

PUT http://localhost/ HTTP/1.0
Host: localhost
Content-type: form-data
Content-length: 90
FileUpload: /Users/SG/files/main.cpp

I was able to write a dummy program that does some PUT from a client, and the web server running Apache returns a HTTP 200. What I am failing to understand currently would be the following two things, which I guess are somewhat connected: 我能够编写一个从客户端执行一些PUT的虚拟程序,运行Apache的Web服务器返回一个HTTP 200.我目前无法理解的是以下两件事,我猜这些内容有些联系:

  1. How one could specify a file to be uploaded from the client in the form above? 如何在上面的表单中指定从客户端上传的文件?

  2. If I understand correctly, the file would be read at client site and then the file content would be sent to the server, where a server side script would read the bytes from client and create a copy of the file at the server. 如果我理解正确,将在客户端站点读取该文件,然后将文件内容发送到服务器,其中服务器端脚本将从客户端读取字节并在服务器上创建该文件的副本。 Is it correct? 这是对的吗?

Any help is appreciated. 任何帮助表示赞赏。

Thanks, 谢谢,
Sayan 萨扬

As stated it's not possible. 如上所述,这是不可能的。 C++ alone has no sockets API. 单独的C ++没有套接字API。 (Edit: with the addition of a BSD sockets API, it's now possible). (编辑:添加了BSD套接字API,现在可以了)。

Your implementation might provide an OS-specific sockets API, in which case your question boils down to, "how do I write an HTTP client?". 您的实现可能会提供特定于操作系统的套接字API,在这种情况下,您的问题归结为“如何编写HTTP客户端?”。 To which the answer is (a) don't, or (b) read the HTTP specification very carefully and do as it tells you. 答案是(a)没有,或(b)非常仔细地阅读HTTP规范并按照它告诉你的那样做。 The basic steps are: 基本步骤是:

  1. (Possibly) parse a URL and use gethostbyname to get an IP address. (可能)解析URL并使用gethostbyname获取IP地址。
  2. Open a TCP socket, using connect . 使用connect打开TCP套接字。
  3. Write the request (you already have an idea what that looks like) using send . 使用send编写请求(您已经知道它看起来像什么)。
  4. Read the response using read . 使用read响应。
  5. Close the connection. 关闭连接。

The difficult part is parsing the response robustly, because there are a lot of features to worry about. 困难的部分是强有力地解析响应,因为有许多功能需要担心。 If your client is tied to a particular server, then its responses will be quite predictable, and you can ignore quite a lot of the HTTP spec (at least until you change the configuration of the server, update its software, etc). 如果您的客户端绑定到特定服务器,那么它的响应将是非常可预测的,您可以忽略相当多的HTTP规范(至少在您更改服务器的配置,更新其软件等之前)。

If you're ready to give up before you finish, there are perfectly good HTTP libraries available, such as libcURL . 如果您准备在完成之前放弃,那么可以使用非常好的HTTP库,例如libcURL

I want to use C++ for doing this, without the help of any libraries or system() calls. 我想使用C ++来做这件事,没有任何库或system()调用的帮助。

Socket programming always requires system calls. 套接字编程总是需要系统调用。 C++ is an extremely general language. C ++是一种非常通用的语言。 The ISO language definition does not specify anything about sockets or network programming, so in order to do this you need to rely on another system-specific library/standard, such as the BSD socket API on UNIX/Linux. ISO语言定义没有指定任何有关套接字或网络编程的内容,因此为了做到这一点,您需要依赖于另一个特定于系统的库/标准,例如UNIX / Linux上的BSD套接字API。 Usually, in C++ you would use a higher-level third-party library that wraps these lower level system calls, such as Boost.ASIO . 通常,在C ++中,您将使用更高级别的第三方库来封装这些较低级别的系统调用,例如Boost.ASIO However, even that takes a bit of learning, to say nothing of correctly implementing the HTTP standard itself on top of your sockets. 但是,即使这需要一些学习,更不用说在套接字上正确实现HTTP标准了。

I was able to attain what I wanted. 我能够达到我想要的。 My http post request string looks like below: 我的http post请求字符串如下所示:

"POST %s HTTP/1.0\r\n" 
"Host: %s\r\n"
"Content-type: multipart/form-data\r\n"
"Content-length: %d\r\n\r\n"
"Content-Disposition: %s; filename: %s\n"

I am sending the above string from a cpp program in client to the server, running Apache, which handles the data through a cgi script, also written in cpp. 我将上述字符串从客户端的cpp程序发送到服务器,运行Apache,它通过cgi脚本处理数据,也用cpp编写。 The data is placed with the Content Disposition attribute. 数据与“ Content Disposition属性一起放置。 Can anybody please point it out to me as to how would I transform the above to a HTTP PUT? 任何人都可以向我指出如何将上述内容转换为HTTP PUT?

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

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