gpt4 book ai didi

php - 使用 C++ HTTP 请求将文件上传到服务器

转载 作者:行者123 更新时间:2023-11-30 05:08:52 33 4
gpt4 key购买 nike

因此,我尝试使用 winsock 从 Windows 7 客户端将文件上传到服务器。为了做到这一点,我设置了套接字,然后创建了一个 HTTP POST 请求,该请求应该将所有数据发送到服务器上的 PHP 文件,然后服务器将上传它。创建请求的代码是...

#include <winsock2.h>
#include <windows.h>
#include <iostream>

using namespace std;
int main (int argc, char* argv[]){
WSADATA wsaData;
char* fileName = argv[1];
WSAStartup(MAKEWORD(2,2), &wsaData);
SOCKET Socket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct hostent *host;
host = gethostbyname("localhost");
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";
connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr);
cout << "Connected.\n";

char *header="POST /upload.php HTTP/1.1\r\n"
"Host: localhost\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";

// Open the existing file.
HANDLE hFile = CreateFile(fileName, GENERIC_READ, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);

int size = GetFileSize(hFile, NULL);

send(Socket,header, strlen(header),0);
//send the file to the server
int bytesSent = 0;
do{
char buff[300];
DWORD dwBytesRead;
//Copy file into array buff
if(!ReadFile(hFile, buff, 300, &dwBytesRead, NULL)){
cout << GetLastError << endl;
return 1;
}
bytesSent += send(Socket, buff, dwBytesRead, 0);
cout << "Bytes Sent " << bytesSent << endl;
}while(bytesSent < size);
char *footer = "\r\n--myboundary--\r\n";
send(Socket, footer, strlen(footer), 0);
closesocket(Socket);
WSACleanup();
cout<<endl<<endl;
return 0;
}

虽然接收文件的 PHP 代码是...

<?php
$target_dir = "recvFile/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if file already exists
if (file_exists($target_file)) {
echo "File already exists";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "File to large";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "ERROR File not uploaded";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "ERROR uploading file";
}
}
?>

但是 $_FILES 中从来没有任何东西,所以上传总是失败。我是否错误地创建了 HTTP 请求?我需要特别说明文件在 myboundary 之后吗?此外,如果有资源包含从头开始创建 HTTP 请求的示例,那将非常有用,因为出于某种原因,wireshark 甚至看不到请求,这使得弄清楚发生了什么变得更加困难。

PS 这是我第一次在 StackOverflow 上提问,所以如果我搞砸了某些格式或行为,请告诉我,以便我进行修复!

最佳答案

我遇到的问题实际上相对容易解决。首先我需要添加一个用户代理,其次我需要定义一个内容长度,它是整个 http 请求的长度(不仅仅是数据部分)。我完成的 HTTP 请求然后显示为:

POST /upload.php HTTP/1.1
Host: localhost
Content-Length: 800
Content-Type: multipart/form-data; boundary=WrbKitFormBoundaryXtRo8bh7ZpnTrTwd
User-Agent: 1337
Connection: keep-alive

--WrbKitFormBoundaryXtRo8bh7ZpnTrTwd
Content-Disposition: form-data; name="fileToUpload"; filename="testMac.cpp"
Content-Type: application/octet-stream

//SEND RAW FILE DATA HERE

--WrbKitFormBoundaryXtRo8bh7ZpnTrTwd

关于php - 使用 C++ HTTP 请求将文件上传到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46550110/

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