要使用C++和WinHttp庫進行文件上傳,請按照以下步驟操作:
#include<iostream>
#include<windows.h>
#include <winhttp.h>
#include <fstream>
#pragma comment(lib, "winhttp")
bool UploadFile(const std::wstring& url, const std::wstring& filePath) {
// 初始化WinHttp會話
HINTERNET hSession = WinHttpOpen(L"MyUserAgent", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
if (!hSession) {
std::cerr << "Error opening session"<< std::endl;
return false;
}
// 連接到服務器
HINTERNET hConnect = WinHttpConnect(hSession, url.c_str(), INTERNET_DEFAULT_HTTPS_PORT, 0);
if (!hConnect) {
std::cerr << "Error connecting to server"<< std::endl;
WinHttpCloseHandle(hSession);
return false;
}
// 打開請求
HINTERNET hRequest = WinHttpOpenRequest(hConnect, L"POST", url.c_str(), NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE);
if (!hRequest) {
std::cerr << "Error opening request"<< std::endl;
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return false;
}
// 設置請求頭
if (!WinHttpAddRequestHeaders(hRequest, L"Content-Type: multipart/form-data; boundary=----MyBoundary\r\n", -1, WINHTTP_ADDREQ_FLAG_ADD)) {
std::cerr << "Error adding headers"<< std::endl;
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return false;
}
// 讀取文件內容
std::ifstream file(filePath, std::ios::binary | std::ios::ate);
if (!file.is_open()) {
std::cerr << "Error opening file"<< std::endl;
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return false;
}
int fileSize = file.tellg();
file.seekg(0, std::ios::beg);
char* buffer = new char[fileSize];
file.read(buffer, fileSize);
file.close();
// 構建請求體
std::string requestBody = "------MyBoundary\r\nContent-Disposition: form-data; name=\"file\"; filename=\"" + std::string(filePath.begin(), filePath.end()) + "\"\r\nContent-Type: application/octet-stream\r\n\r\n";
requestBody += std::string(buffer, fileSize);
requestBody += "\r\n------MyBoundary--\r\n";
delete[] buffer;
// 發(fā)送請求
if (!WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, (LPVOID)requestBody.c_str(), requestBody.size(), requestBody.size(), 0)) {
std::cerr << "Error sending request"<< std::endl;
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return false;
}
// 接收響應
if (!WinHttpReceiveResponse(hRequest, NULL)) {
std::cerr << "Error receiving response"<< std::endl;
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return false;
}
// 獲取響應狀態(tài)碼
DWORD statusCode = 0;
DWORD statusCodeSize = sizeof(statusCode);
if (!WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, NULL, &statusCode, &statusCodeSize, NULL)) {
std::cerr << "Error querying headers"<< std::endl;
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return false;
}
// 關閉句柄
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
// 檢查狀態(tài)碼
if (statusCode != 200) {
std::cerr << "Server returned status code: "<< statusCode<< std::endl;
return false;
}
return true;
}
UploadFile
函數(shù):int main() {
std::wstring url = L"https://example.com/upload";
std::wstring filePath = L"C:\\path\\to\\your\\file.txt";
if (UploadFile(url, filePath)) {
std::cout << "File uploaded successfully!"<< std::endl;
} else {
std::cerr << "Error uploading file"<< std::endl;
}
return 0;
}
這個示例將文件上傳到指定的URL。請確保將url
和filePath
變量更改為實際值。注意,這個示例僅適用于Windows平臺。