91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

如何利用C++ WinHttp進行文件上傳

c++
小樊
122
2024-08-29 16:59:09
欄目: 編程語言

要使用C++和WinHttp庫進行文件上傳,請按照以下步驟操作:

  1. 包含所需的頭文件:
#include<iostream>
#include<windows.h>
#include <winhttp.h>
#include <fstream>
#pragma comment(lib, "winhttp")
  1. 定義一個函數來執行文件上傳:
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;

    // 發送請求
    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;
    }

    // 獲取響應狀態碼
    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);

    // 檢查狀態碼
    if (statusCode != 200) {
        std::cerr << "Server returned status code: "<< statusCode<< std::endl;
        return false;
    }

    return true;
}
  1. 在主函數中調用UploadFile函數:
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。請確保將urlfilePath變量更改為實際值。注意,這個示例僅適用于Windows平臺。

0
通山县| 两当县| 云南省| 华坪县| 修武县| 镇沅| 仙居县| 南陵县| 余姚市| 峨山| 武陟县| 吉林省| 新和县| 桂林市| 黑山县| 扎鲁特旗| 酉阳| 正阳县| 汉阴县| 石狮市| 安仁县| 勐海县| 河曲县| 海林市| 崇阳县| 金川县| 晴隆县| 紫阳县| 白玉县| 南溪县| 石门县| 沙洋县| 万载县| 牙克石市| 双鸭山市| 呼玛县| 横山县| 伽师县| 抚州市| 通化市| 南郑县|