PHP & C++ $_FILES 和 HttpSendRequestA
PHP & C++ $_FILES and HttpSendRequestA
将文件从c++上传到PHP表单时遇到问题。Iv看到了与我的问题类似的帖子。
- 用C上传图像文件++
- 使用C++HTTP请求将文件上载到服务器
根据每个和HERE中列出的文档,所有帐户的代码都应该是正确的。
我也不能将CURL用于我的实现。我也愿意。
Iv尝试将所有数据编码到BASE64中,以缓解任何文件数据问题,但没有成功。
Iv仔细检查了我的php.ini文件以允许上传。并用一个简单的HTML页面测试了PHP页面,该页面允许我上传文件。一切都很好,表格给出了正确的结果。
基本上,我发送到服务器的标题是:
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.');
}
最后我的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 += "--WrbKitFormBoundaryXtRo8bh7ZpnTrTwdrn";
datahead += "Content-Disposition: form-data; name="file"; filename="" + file.filename().string() +""rn"; //Content - Disposition : form - data; name = "fileToUpload"; filename = "testMac.cpp"
datahead += "Content-Type: text/plain;rn";
datahead += "Content-Transfer-Encoding: BASE64rnn";
datahead += content;
datahead += "rn--WrbKitFormBoundaryXtRo8bh7ZpnTrTwd--rn";
//datahead = base64_encode(reinterpret_cast<const unsigned char*>(datahead.c_str()), datahead.length());
//Build Headder
headder += "Host: " + iaddr + "rn"; //Host: localhost
headder += "Connection: closern"; //Connection : keep - alive
headder += "Content-Length: " + std::to_string(datahead.size()) + "rn"; //Content - Length : 800
headder += "Origin: http://dzwxgames.comrn";
//headder += "Content-Transfer-Encoding: base64rn";
headder += "Content-Type: multipart/form-data; boundary=WrbKitFormBoundaryXtRo8bh7ZpnTrTwdrn"; //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.36rn"; //User - Agent: 1337
headder += "Pragma: no-cachernrn";
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;
}
我错过了什么?我似乎找不出问题。
在与wireshark进行检查后,我发现一个301错误标头被发送回我,并强制升级到HTTPS。在删除HTTPS进行测试后,这修复了错误。现在我只需要修复上传时的错误代码3。欧。
由于某种原因,HttpQueryInfo没有报告错误301。相反,它将数据包重新发送到新地址,但如果数据没有加密,它不知道该怎么处理
有趣的是,Apache默默地丢弃了未加密的数据,因为它无法加密。日志中没有任何内容,也没有提及。古怪的
不确定是否有意。但是好的。
相关文章:
- 为什么 C++ 编译器在"c:program filesgnu emacsinclude"中查找包含文件?
- MFC 对话框控件的"Accept Files"选项如何工作?
- 以简单格式"c:filessample.txt"从用户读取文件路径
- /usr/include/boost/filesystem/path.hpp:307:未定义对'boost::files
- MinGW在代码块中构建时的ar.exe "could not create temporary file whilst writing archive: no more archived files
- 标准Windows 7用户如何能够删除C:Program Files下的文件
- 通过QT播放mov-files
- 错误 PRJ0002:错误结果 -1073741515 从"C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\cl.
- 使用运算符"+" "Merging two files"
- header / cpp files
- 如何在Program Files文件夹中复制文件或文件夹
- 写入 C:Program Files 中包含的文件夹的文件名
- "Too Many Files" Nexus 5/6棒棒糖上的错误
- CMake and Window .Lib Files
- 在Qt安装程序框架中将"Program Files"设置为默认安装目录
- GCC/MINGW中'startup files'来源
- C++ Class Files
- 项目根目录中的生成文件 "no Input Files"
- boost::files系统:带筛选器的递归目录迭代器
- C++:在'#include <files>'之前是否允许命名空间定义。