PHP&;C++$_文件和HttpSendRequestA 从C++上传文件到PHP窗体有问题。我看到了与我的问题类似的帖子
并根据每个文件中列出的内容,在我的代码中所有帐户都应该是正确的 我也不能在实现中使用CURL。我也愿意 Iv尝试将所有数据编码到BASE64以缓解任何文件数据问题,但没有成功 Iv仔细检查了我的php.ini文件以允许上传。并用一个简单的HTML页面测试了PHP页面,该页面允许我上传一个文件。都很好,表格给出了正确的结果 基本上,我发送到服务器的头是:PHP&;C++$_文件和HttpSendRequestA 从C++上传文件到PHP窗体有问题。我看到了与我的问题类似的帖子,php,c++,windows,http-headers,Php,C++,Windows,Http Headers,并根据每个文件中列出的内容,在我的代码中所有帐户都应该是正确的 我也不能在实现中使用CURL。我也愿意 Iv尝试将所有数据编码到BASE64以缓解任何文件数据问题,但没有成功 Iv仔细检查了我的php.ini文件以允许上传。并用一个简单的HTML页面测试了PHP页面,该页面允许我上传一个文件。都很好,表格给出了正确的结果 基本上,我发送到服务器的头是: Host: dzwxgames.com Connection: close Content-Length: 418 Origin: http:/
Host: dzwxgames.com
Connection: close
Content-Length: 418
Origin: http://dzwxgames.com
Content-Type: multipart/form-data; boundary=WrbKitFormBoundaryXtRo8bh7ZpnTrTwd
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36
Pragma: no-cache
--WrbKitFormBoundaryXtRo8bh7ZpnTrTwd
Content-Disposition: form-data; name="file"; filename="02-11-2019 09-44.log"
Content-Type: text/plain;
Content-Transfer-Encoding: BASE64
W0FsdF1bVGFiXVtUYWJdW1RhYl1bQ11bRF1bU3BhY2VdW1VdW1BdW1RhYl1bRW50ZXJdW0xdW1NdW0VudGVyXVtDXVtEXVtTcGFjZV1bLl1bLl1bL11bRW50ZXJdW0xdW1NdW0VudGVyXVtBbHRdW1RhYl1bVGFiXVtUYWJdW1RhYl1bQWx0XVtUYWJdW1RhYl0=
--WrbKitFormBoundaryXtRo8bh7ZpnTrTwd--
我的PHP页面将返回:
HTTP/1.1 200 OK
Date: Sun, 03 Nov 2019 09:09:19 GMT
Server: Apache/2.4.29 (Ubuntu)
Content-Length: 35
Connection: close
Content-Type: text/plain; charset=utf-8
NULL
Array
(
)
File does not exist.
php页面的代码很简单:
if (!isset($_FILES['file']['error'])){
var_dump($_FILES['file']);
print_r($_FILES);
throw new RuntimeException('File does not exist.');
}
P> >我的C++代码是:
bool sendFileToRemoteServer(std::filesystem::path file, const std::string& url,const std::string& iaddr) {
std::string headder = ""; //Headder information
std::string datahead = "";
std::string content = "";
if (!File::fastFileToString(file.string(), content)) {
std::cout << "ERROR_FILE_OPEN" << std::endl;
return false;
}
content = base64_encode(reinterpret_cast<const unsigned char*>(content.c_str()),content.length());
datahead += "--WrbKitFormBoundaryXtRo8bh7ZpnTrTwd\r\n";
datahead += "Content-Disposition: form-data; name=\"file\"; filename=\"" + file.filename().string() +"\"\r\n"; //Content - Disposition : form - data; name = "fileToUpload"; filename = "testMac.cpp"
datahead += "Content-Type: text/plain;\r\n";
datahead += "Content-Transfer-Encoding: BASE64\r\n\n";
datahead += content;
datahead += "\r\n--WrbKitFormBoundaryXtRo8bh7ZpnTrTwd--\r\n";
//datahead = base64_encode(reinterpret_cast<const unsigned char*>(datahead.c_str()), datahead.length());
//Build Headder
headder += "Host: " + iaddr + "\r\n"; //Host: localhost
headder += "Connection: close\r\n"; //Connection : keep - alive
headder += "Content-Length: " + std::to_string(datahead.size()) + "\r\n"; //Content - Length : 800
headder += "Origin: http://dzwxgames.com\r\n";
//headder += "Content-Transfer-Encoding: base64\r\n";
headder += "Content-Type: multipart/form-data; boundary=WrbKitFormBoundaryXtRo8bh7ZpnTrTwd\r\n"; //Content - Type : multipart / form - data; boundary = WrbKitFormBoundaryXtRo8bh7ZpnTrTwd
headder += "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36\r\n"; //User - Agent: 1337
headder += "Pragma: no-cache\r\n\r\n";
std::cout << "SENT HEAD:" << std::endl;
std::cout << headder;
std::cout << datahead;
//Open internet connection
HINTERNET hSession = InternetOpen("WINDOWS", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hSession == NULL){
printLastError(GetLastError());
printf("ERROR_INTERNET_OPEN");
return false;
}
HINTERNET hConnect = InternetConnect(hSession, iaddr.c_str(), INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
if (hConnect == NULL){
printLastError(GetLastError());
printf("ERROR_INTERNET_CONN");
return false;
}
HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST", _T(url.c_str()), NULL, NULL, NULL, INTERNET_FLAG_RELOAD, 1);
if (hRequest == NULL){
printLastError(GetLastError());
printf("ERROR_INTERNET_REQ");
return false;
}
if (!HttpSendRequestA(hRequest, headder.c_str(), headder.size(), (void*)datahead.c_str(), datahead.size())){
printLastError(GetLastError());
printf("ERROR_INTERNET_SEND");
return false;
}
//Request Done, get responce
DWORD dwSize = 0;
if (!InternetQueryDataAvailable(hRequest, &dwSize, 0, 0)) {
printLastError(GetLastError());
printf("ERROR_InternetQueryDataAvailable");
return false;
}
char* responce = new char[dwSize + 1];
DWORD dwlpsizeread = 0;
if (!InternetReadFile(hRequest, responce, dwSize, &dwlpsizeread)) {
printLastError(GetLastError());
printf("ERROR_InternetReadFile");
return false;
}
printf("\nRESPONCE HEAD : \n");
SampleCodeOne(hRequest);
responce[dwSize] = 0;
std::cout << responce << std::endl;
delete[] responce;
InternetCloseHandle(hSession);
InternetCloseHandle(hConnect);
InternetCloseHandle(hRequest);
return true;
}
bool sendFileToRemoteServer(std::filesystem::path file,const std::string&url,const std::string&iaddr){
std::string header=”“;//header信息
std::string datahead=“”;
std::string content=“”;
如果(!File::fastFileToString(File.string(),content)){
std::cout在与wire shark进行检查后,我发现一个301错误头发送回我,我强制升级到HTTPS。在删除HTTPS进行测试后,这修复了错误。现在我只需修复上传时的错误代码3。EUGH
出于某种原因,HttpQueryInfo没有报告错误301。相反,它将数据包重新发送到新地址,但在数据未加密的情况下,它不知道如何处理它
有趣的是,Apache悄悄地丢弃了未加密的数据,因为它无法加密。日志中没有任何内容,没有提及。奇怪
不确定是否有意。但确定。只有在通过浏览器上载时才需要文件上载请求格式。否则,只需将文件作为原始帖子正文发送,并将正文写入服务器上的文件。名称可以是url参数。