• winhttp 发送https的get请求


    来自:https://docs.microsoft.com/en-us/windows/win32/api/winhttp/nf-winhttp-winhttpconnect

    DWORD dwSize = 0;
        DWORD dwDownloaded = 0;
        LPSTR pszOutBuffer;
        BOOL  bResults = FALSE;
        HINTERNET  hSession = NULL, 
                   hConnect = NULL,
                   hRequest = NULL;
    
        // Use WinHttpOpen to obtain a session handle.
        hSession = WinHttpOpen( L"WinHTTP Example/1.0",  
                                WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
                                WINHTTP_NO_PROXY_NAME, 
                                WINHTTP_NO_PROXY_BYPASS, 0);
    
        // Specify an HTTP server.
        if (hSession)
            hConnect = WinHttpConnect( hSession, L"www.microsoft.com",
                                       INTERNET_DEFAULT_HTTPS_PORT, 0);
    
        // Create an HTTP request handle.
        if (hConnect)
            hRequest = WinHttpOpenRequest( hConnect, L"GET", NULL,
                                           NULL, WINHTTP_NO_REFERER, 
                                           WINHTTP_DEFAULT_ACCEPT_TYPES, 
                                           WINHTTP_FLAG_SECURE);
    
        // Send a request.
        if (hRequest)
            bResults = WinHttpSendRequest( hRequest,
                                           WINHTTP_NO_ADDITIONAL_HEADERS,
                                           0, WINHTTP_NO_REQUEST_DATA, 0, 
                                           0, 0);
    
     
        // End the request.
        if (bResults)
            bResults = WinHttpReceiveResponse( hRequest, NULL);
    
        // Keep checking for data until there is nothing left.
        if (bResults)
            do 
            {
                // Check for available data.
                dwSize = 0;
                if (!WinHttpQueryDataAvailable( hRequest, &dwSize))
                    printf("Error %u in WinHttpQueryDataAvailable.
    ", GetLastError());
    
                // Allocate space for the buffer.
                pszOutBuffer = new char[dwSize+1];
                if (!pszOutBuffer)
                {
                    printf("Out of memory
    ");
                    dwSize=0;
                }
                else
                {
                    // Read the Data.
                    ZeroMemory(pszOutBuffer, dwSize+1);
    
                    if (!WinHttpReadData( hRequest, (LPVOID)pszOutBuffer, 
                                          dwSize, &dwDownloaded))
                        printf( "Error %u in WinHttpReadData.
    ", GetLastError());
                    else
                        printf( "%s
    ", pszOutBuffer);
                
                    // Free the memory allocated to the buffer.
                    delete [] pszOutBuffer;
                }
    
            } while (dwSize > 0);
    
    
        // Report any errors.
        if (!bResults)
            printf("Error %d has occurred.
    ", GetLastError());
    
        // Close any open handles.
        if (hRequest) WinHttpCloseHandle(hRequest);
        if (hConnect) WinHttpCloseHandle(hConnect);
        if (hSession) WinHttpCloseHandle(hSession);
  • 相关阅读:
    【转】ANSI与GB2312的编码问题
    asp.net相关文件后缀名都是什么意思?
    【转】HTTP 通信http通信过程简介
    【笔记】【转载】设计模式
    【转载】DataGridView之将数据导出成Excel和Word格式
    新开博客
    ASP.NET 从Excel文件导入数据到数据库
    Asp.net 图片异步上传的简单实现
    【笔记】【转载】JohnConnor设计模式笔记(一) 学习设计模式之前你必须掌握的-看懂UML类图
    boost Smart Pointer
  • 原文地址:https://www.cnblogs.com/Galesaur-wcy/p/15328783.html
Copyright © 2020-2023  润新知