使用WinInet实现HTTP站点访问

  • 时间:
  • 来源:互联网
  • 文章标签:

废话不多说了,直接上代码

HTTP的GET方式代码

void sendGetRequest(LPCTSTR lpszURL)
{
    LPCTSTR lpszAgent = _T("WininetGet/0.1");

    HINTERNET hInternet = NULL;
    HINTERNET hConnect = NULL;
    HINTERNET hRequest = NULL;
    do 
    {
        CString strServer, strObject;
        DWORD dwServerType;
        INTERNET_PORT nPort;
        if (!AfxParseURL(lpszURL, dwServerType, strServer, strObject, nPort))
        {
            break;
        }
        hInternet = InternetOpen(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
        if (NULL==hInternet)
        {
            break;
        }

        LPCTSTR lpszUserName = NULL;
        LPCTSTR lpszPassword = NULL;
        DWORD dwConnectFlags = 0, dwConnectContext = 0;
        hConnect = InternetConnect(hInternet, strServer, nPort,
            lpszUserName, lpszPassword,
            INTERNET_SERVICE_HTTP,
            dwConnectFlags, dwConnectContext);
        if (NULL==hConnect)
        {
            break;
        }

        LPCTSTR lpszVerb = _T("GET");
        LPCTSTR lpszVersion = NULL;
        LPCTSTR lpszReferrer = NULL;
        LPCTSTR *lplpszAcceptTypes = NULL;  // 允许接受所有类型
        DWORD dwOpenRequestFlags = INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP |
            INTERNET_FLAG_KEEP_CONNECTION |
            INTERNET_FLAG_NO_AUTH |
            INTERNET_FLAG_NO_COOKIES |
            INTERNET_FLAG_NO_UI |
            INTERNET_FLAG_IGNORE_CERT_CN_INVALID |
            INTERNET_FLAG_RELOAD;
        DWORD dwOpenRequestContext = 0;
        hRequest = HttpOpenRequest(hConnect, lpszVerb, strObject, lpszVersion,
            lpszReferrer, lplpszAcceptTypes,
            dwOpenRequestFlags, dwOpenRequestContext);
        if (NULL==hRequest)
        {
            break;
        }
        
        BOOL bResult = HttpSendRequest(hRequest, NULL, 0, NULL, 0);
        if (!bResult)
        {
            break;
        }

        // 获取 HTTP应答头部信息
        TCHAR szBuff[TRANSFER_SIZE];
        DWORD dwReadSize = TRANSFER_SIZE;
        bResult = HttpQueryInfo(hRequest, HTTP_QUERY_RAW_HEADERS, szBuff, &dwReadSize, NULL);
        if (!bResult)
        {
            break;
        }
        szBuff[dwReadSize] = _T('\0');
        // 获取 HTTP应答包体信息
        DWORD dwByteAvailable = 0;
        bResult = InternetQueryDataAvailable(hRequest, &dwByteAvailable, 0, 0);
        if (!bResult)
        {
            break;
        }
        char szBody[TRANSFER_SIZE];
        DWORD dwBytesRead = 0;
        bResult = InternetReadFile(hRequest, szBody, dwByteAvailable, &dwBytesRead);
        if (!bResult)
        {
            break;
        }
        szBody[dwBytesRead] = '\0';
        TRACE(szBuff);
    } while (false);
    if (NULL != hRequest)
    {
        InternetCloseHandle(hRequest);
    }
    if (NULL!=hConnect)
    {
        InternetCloseHandle(hConnect);
    }
    if (NULL != hInternet)
    {
        InternetCloseHandle(hInternet);
    }
}

转载于:https://www.cnblogs.com/sanghg/p/5475006.html

本文链接http://www.taodudu.cc/news/show-647612.html