117.info
人生若只如初见

使用WinINet和WinHTTP实现Http访问

使用WinINet实现Http访问:

#include 
#include 
#include 

int main() {
    HINTERNET hInternet;
    HINTERNET hConnect;
    DWORD bytesRead;
    char buffer[4096];

    // 初始化WinINet
    hInternet = InternetOpen(L"WinINet Example", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
    if (hInternet == NULL) {
        std::cout << "Failed to initialize WinINet: " << GetLastError() << std::endl;
        return 1;
    }
    
    // 打开Http连接
    hConnect = InternetOpenUrlA(hInternet, "http://www.example.com", NULL, 0, INTERNET_FLAG_RELOAD, 0);
    if (hConnect == NULL) {
        std::cout << "Failed to open Http connection: " << GetLastError() << std::endl;
        return 1;
    }
    
    // 读取响应数据
    while (InternetReadFile(hConnect, buffer, sizeof(buffer), &bytesRead) && bytesRead > 0) {
        std::cout.write(buffer, bytesRead);
    }
    
    // 关闭连接和WinINet
    InternetCloseHandle(hConnect);
    InternetCloseHandle(hInternet);
    
    return 0;
}

使用WinHTTP实现Http访问:

#include 
#include 
#include 

int main() {
    HINTERNET hSession;
    HINTERNET hConnect;
    DWORD bytesRead;
    char buffer[4096];

    // 初始化WinHTTP
    hSession = WinHttpOpen(L"WinHTTP Example", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
    if (hSession == NULL) {
        std::cout << "Failed to initialize WinHTTP: " << GetLastError() << std::endl;
        return 1;
    }

    // 打开Http连接
    hConnect = WinHttpOpenRequest(hSession, L"GET", L"http://www.example.com", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
    if (hConnect == NULL) {
        std::cout << "Failed to open Http connection: " << GetLastError() << std::endl;
        WinHttpCloseHandle(hSession);
        return 1;
    }

    // 发送请求
    if (!WinHttpSendRequest(hConnect, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0)) {
        std::cout << "Failed to send Http request: " << GetLastError() << std::endl;
        WinHttpCloseHandle(hConnect);
        WinHttpCloseHandle(hSession);
        return 1;
    }

    // 接收响应
    if (!WinHttpReceiveResponse(hConnect, NULL)) {
        std::cout << "Failed to receive Http response: " << GetLastError() << std::endl;
        WinHttpCloseHandle(hConnect);
        WinHttpCloseHandle(hSession);
        return 1;
    }

    // 读取响应数据
    while (WinHttpReadData(hConnect, buffer, sizeof(buffer), &bytesRead) && bytesRead > 0) {
        std::cout.write(buffer, bytesRead);
    }

    // 关闭连接和WinHTTP
    WinHttpCloseHandle(hConnect);
    WinHttpCloseHandle(hSession);

    return 0;
}

未经允许不得转载 » 本文链接:https://www.117.info/ask/febd3AzsLAwRRB10.html

推荐文章

  • linux 客户端 Socket 非阻塞connect编程

    在Linux中,可以使用非阻塞IO(Non-blocking IO)来实现非阻塞connect的编程。
    以下是一个简单的示例代码,演示如何在Linux中使用非阻塞connect进行Socket编...

  • Android常用控件Spinner的使用

    Spinner是Android开发中常用的控件之一,用于显示下拉菜单的选项。以下是Spinner的使用方法: 在XML布局文件中添加Spinner控件: 在Java代码中获取Spinner控件的...

  • Ubuntu中安装gcc编译器

    要在Ubuntu中安装GCC编译器,您可以按照以下步骤进行操作: 打开终端 (Ctrl + Alt + T)。
    运行以下命令来更新包列表:sudo apt update
    运行以下命令来...

  • mysql中text,longtext,mediumtext区别

    在MySQL中,text、longtext和mediumtext都是用于存储文本数据类型的列。它们之间的区别如下: text:text类型可以存储最大长度为65,535个字符(约为64KB)的文本...