使用c ++通过http POST发送文件

The*_*tor 3 c++ post http http-headers

#include <winsock2.h>
#include <windows.h>
#include <iostream>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
int main (){
    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
        cout << "WSAStartup failed.\n";
        system("pause");
        return 1;
    }
    SOCKET Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    struct hostent *host;
    host = gethostbyname("127.0.0.1");
    SOCKADDR_IN SockAddr;
    SockAddr.sin_port=htons(80);
    SockAddr.sin_family=AF_INET;
    SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
    cout << "Connecting...\n";
    if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr)) != 0){
        cout << "Could not connect";
        system("pause");
        return 1;
    }
    cout << "Connected.\n";



          char header[]="POST /xampp/tests/file/check.php HTTP/1.1\r\n"
              "Host: 127.0.0.1\r\n"
              "Content-Type: application/x-www-form-urlencoded\r\n"
              "Content-Length: 10\r\n"
              "Connection: close\r\n"
              "\r\n"
              "text1=sase";
    send(Socket,header, strlen(header),0);
    char buffer[100000];
    int nDataLength;
    while ((nDataLength = recv(Socket,buffer,100000,0)) > 0){        
        int i = 0;
        while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
            cout << buffer[i];
            i += 1;
        }
    }
    closesocket(Socket);
        WSACleanup();
        cout<<endl<<endl;
    system("pause");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是我当前的代码。它发送text1,但现在我希望它发送一个文件(位于:C:\ Users \ Wade \ Downloads \ Documents)。我该如何使用HTTP POST协议将文件从用户发送到服务器

Rem*_*eau 5

application/x-www-form-urlencoded仅支持name=value配对。对于POST文件,您必须:

  1. 使用multipart/form-data代替。

    char *header="POST /xampp/tests/file/check.php HTTP/1.1\r\n"
          "Host: 127.0.0.1\r\n"
          "Content-Type: multipart/form-data; boundary=myboundary\r\n"
          "Connection: close\r\n"
          "\r\n"
          "--myboundary\r\n"
          "Content-Type: application/octet-stream\r\n"
          "Content-Disposition: form-data; name=\"myfile\"; filename=\"myfile.ext\"\r\n"
          "Content-Transfer-Encoding: 8bit\r\n"
          "\r\n";
    send(Socket,header, strlen(header),0);
    
    // send the raw file bytes here...
    
    char *footer = "\r\n"
               "--myboundary--\r\n";
    send(Socket, footer, strlen(footer), 0);
    
    Run Code Online (Sandbox Code Playgroud)
  2. 单独发送文件内容作为全部POST内容,将设置Content-Type为文件的实际类型或application/octet-stream,然后将设置Content-Length为文件的大小。

    char *header="POST /xampp/tests/file/check.php HTTP/1.1\r\n"
          "Host: 127.0.0.1\r\n"
          "Content-Type: application/octet-stream\r\n"
          "Content-Length: ...\r\n" // <-- substitute with the actual file size
          "Connection: close\r\n"
          "\r\n";
    send(Socket,header, strlen(header),0);
    
    // send the raw file bytes here...
    
    Run Code Online (Sandbox Code Playgroud)

您使用哪种服务器取决于服务器能够接受的功能。