zoukankan      html  css  js  c++  java
  • 使用Windows API发送HTTP请求

    先看一个简单的GET示例

    #include <Windows.h>
    #include <winhttp.h>
    #include <stdio.h>
    int main()
    {
        HINTERNET sessionHandle = WinHttpOpen(L"WinHttp Example", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
        if (sessionHandle)
        {
            HINTERNET connectionHandle = WinHttpConnect(sessionHandle, L"example.com", INTERNET_DEFAULT_HTTP_PORT, 0);
            if (connectionHandle)
            {
                HINTERNET requestHandle = WinHttpOpenRequest(connectionHandle, L"GET", L"", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
                if (requestHandle)
                {
                    BOOL success = WinHttpSendRequest(requestHandle, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
                    if (success)
                    {
                        success = WinHttpReceiveResponse(requestHandle, NULL);
                        if (success)
                        {
                            DWORD dwSize;
                            do
                            {
                                dwSize = 0;
                                LPSTR pszOutBuffer;
                                DWORD dwDownloaded = 0;
                                if (!WinHttpQueryDataAvailable(requestHandle, &dwSize))
                                {
                                    printf("Error %u in WinHttpQueryDataAvailable.
    ", GetLastError());
                                    break;
                                }
                                // No more available data.
                                if (!dwSize)
                                    break;
                                // Allocate space for the buffer.
                                pszOutBuffer = new char[dwSize + 1];
                                if (!pszOutBuffer)
                                {
                                    printf("Out of memory
    ");
                                    break;
                                }
                                // Read the Data.
                                ZeroMemory(pszOutBuffer, dwSize + 1);
                                if (!WinHttpReadData(requestHandle, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded))
                                {
                                    printf("Error %u in WinHttpReadData.
    ", GetLastError());
                                }
                                else
                                {
                                    printf("%s", pszOutBuffer);
                                }
                                // Free the memory allocated to the buffer.
                                delete[] pszOutBuffer;
                                // This condition should never be reached since WinHttpQueryDataAvailable
                                // reported that there are bits to read.
                                if (!dwDownloaded)
                                    break;
                            } while (dwSize > 0);
                        }
                        else
                        {
                            // Report any errors.
                            printf("Error %d has occurred.
    ", GetLastError());
                        }
                    }
                    else
                    {
                        printf("Request failed
    ");
                    }
                    WinHttpCloseHandle(requestHandle);
                }
                else
                {
                    printf("Invalid request handle
    ");
                }
                WinHttpCloseHandle(connectionHandle);
            }
            else
            {
                printf("Invalid connection handle
    ");
            }
            WinHttpCloseHandle(sessionHandle);
        }
        else
        {
            printf("Invalid WinHTTP-session handle
    ");
        }
        system("pause");
        return 0;
    }

    有没有一种要崩溃的节奏仅从example.com获取网页代码,就要写约90行的代码

    更麻烦的不是代码量的问题,而是这些API暴露了太多的细节(当然,暴露细节的好处是不限制开发员的思想,根据需求灵活编码)。很多时候,我们并不需要考虑那么多的细节

    试着封装成C++类的形式,然而我放弃了,真的太麻烦了还是用libcurl或cpr之类的库吧

    参考链接:WinHttpReadData function | Microsoft Docs

  • 相关阅读:
    Matlab 整数线性规划问题模型代码
    Matlab 非线性规划问题模型代码
    Matlab 线性规划问题模型代码
    多波次导弹发射中的规划问题(二) 问题一解答
    多波次导弹发射中的规划问题(一) 网络图绘制及数据整理
    多无人机对组网雷达的协同干扰问题 数学建模
    余胜威《MATLAB数学建模经典案例实战》2015年版
    袁新生《LINGO和Excel在数学建模中的应用》
    卓金武《MATLAB在数学建模中的应用》 第2版
    司守奎《数学建模算法与应用》 第二版
  • 原文地址:https://www.cnblogs.com/buyishi/p/10400311.html
Copyright © 2011-2022 走看看