亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

C++ WinHttp接收大數據流的方法

c++
小樊
88
2024-08-29 16:56:42
欄目: 編程語言

在C++中,使用WinHttp庫接收大數據流的方法如下:

  1. 首先,包含所需的頭文件并鏈接到WinHttp庫。
#include<iostream>
#include<vector>
#include<Windows.h>
#include <Winhttp.h>
#pragma comment(lib, "winhttp")
  1. 定義一個函數來初始化WinHttp會話、連接和請求。
HINTERNET InitializeWinHttp(const std::wstring& url) {
    HINTERNET hSession = WinHttpOpen(L"MyUserAgent", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
    if (!hSession) {
        return NULL;
    }

    URL_COMPONENTS urlComponents;
    ZeroMemory(&urlComponents, sizeof(urlComponents));
    urlComponents.dwStructSize = sizeof(urlComponents);
    urlComponents.dwSchemeLength = -1;
    urlComponents.dwHostNameLength = -1;
    urlComponents.dwUrlPathLength = -1;

    if (!WinHttpCrackUrl(url.c_str(), static_cast<DWORD>(url.length()), 0, &urlComponents)) {
        WinHttpCloseHandle(hSession);
        return NULL;
    }

    HINTERNET hConnect = WinHttpConnect(hSession, urlComponents.lpszHostName, urlComponents.nPort, 0);
    if (!hConnect) {
        WinHttpCloseHandle(hSession);
        return NULL;
    }

    HINTERNET hRequest = WinHttpOpenRequest(hConnect, L"GET", urlComponents.lpszUrlPath, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
    if (!hRequest) {
        WinHttpCloseHandle(hSession);
        WinHttpCloseHandle(hConnect);
        return NULL;
    }

    return hRequest;
}
  1. 定義一個函數來接收大數據流。
bool ReceiveLargeDataStream(const std::wstring& url, std::vector<char>& data) {
    HINTERNET hRequest = InitializeWinHttp(url);
    if (!hRequest) {
        return false;
    }

    if (!WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0)) {
        WinHttpCloseHandle(hRequest);
        return false;
    }

    if (!WinHttpReceiveResponse(hRequest, NULL)) {
        WinHttpCloseHandle(hRequest);
        return false;
    }

    DWORD dwStatusCode = 0;
    DWORD dwSize = sizeof(dwStatusCode);
    if (!WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, NULL, &dwStatusCode, &dwSize, NULL)) {
        WinHttpCloseHandle(hRequest);
        return false;
    }

    if (dwStatusCode != HTTP_STATUS_OK) {
        WinHttpCloseHandle(hRequest);
        return false;
    }

    char buffer[4096];
    DWORD dwBytesRead;
    do {
        if (!WinHttpReadData(hRequest, buffer, sizeof(buffer), &dwBytesRead)) {
            WinHttpCloseHandle(hRequest);
            return false;
        }

        if (dwBytesRead == 0) {
            break;
        }

        data.insert(data.end(), buffer, buffer + dwBytesRead);
    } while (true);

    WinHttpCloseHandle(hRequest);
    return true;
}
  1. 在主函數中調用ReceiveLargeDataStream函數。
int main() {
    std::wstring url = L"https://example.com/large-data-stream";
    std::vector<char> data;

    if (ReceiveLargeDataStream(url, data)) {
        std::cout << "Received data size: "<< data.size()<< std::endl;
    } else {
        std::cerr << "Failed to receive data stream."<< std::endl;
    }

    return 0;
}

這個示例將從指定的URL接收大數據流,并將其存儲在std::vector<char>中。你可以根據需要修改此代碼以處理接收到的數據。

0
谢通门县| 锡林郭勒盟| 临汾市| 沅陵县| 枞阳县| 军事| 卢氏县| 辽源市| 宜兰县| 武穴市| 平罗县| 喀喇沁旗| 千阳县| 东丽区| 周宁县| 扶沟县| 越西县| 三亚市| 炉霍县| 从江县| 泸定县| 襄城县| 井陉县| 甘德县| 桑植县| 霸州市| 开远市| 义马市| 海淀区| 岑巩县| 鄂伦春自治旗| 车险| 壤塘县| 西青区| 宜州市| 桓仁| 望江县| 中江县| 滦南县| 无锡市| 潢川县|