基本知识

HTTP(超文本传输协议)
HTTPS(超文本传输安全协议)
URL(统一资源定位器)

例子

1. 一个简单的例子

#include <afxinet.h>
#include <string>

bool testHttp(const CString& strUrl, const BYTE* data, int nBytes, CString& strReceive)
{
    // 解析Url
    // 例如:https://www.cnblogs.com/Zender/p/7596730.html
    DWORD dwServiceType;        // 协议类型:http/https    AFX_INET_SERVICE_HTTP (3)    AFX_INET_SERVICE_HTTPS (4107)
    CString strServer;            // Host    例如:www.cnblogs.com 或者 192.168.10.9
    CString strObject;            // other   例如:/Zender/p/7596730.html
    INTERNET_PORT wPort;        // 端口号   http的默认端口号是80,https的默认端口号是443
    if (!AfxParseURL(strUrl, dwServiceType, strServer, strObject, wPort))
    {
        //解析Url失败
        return false;
    }
    CInternetSession session; // 创建会话
    const int nTimeOut = 10 * 1000;  // 超时设置为 10s
    session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut);
    session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 2);
    CHttpConnection* pConn = NULL;
    CHttpFile* pFile = NULL;
    if (AFX_INET_SERVICE_HTTP == dwServiceType)
    {
        pConn = session.GetHttpConnection(strServer, wPort);
        pFile = pConn->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject);
    }
    else if (AFX_INET_SERVICE_HTTPS == dwServiceType)
    {
        pConn = session.GetHttpConnection(strServer, INTERNET_FLAG_SECURE, wPort);
        pFile = pConn->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject, NULL, 1, NULL, NULL,
            INTERNET_FLAG_SECURE | INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_RELOAD |
            INTERNET_FLAG_IGNORE_CERT_CN_INVALID | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID);//SECURITY_FLAG_IGNORE_UNKNOWN_CA
    }
    else
    {
        //其他协议返回
        return false;
    }
    try
    {
        CString strHeaders("Content-Type: application/x-www-form-urlencoded");//请求头
        BOOL bRet = pFile->SendRequest(strHeaders, (LPVOID)data, nBytes);
        if (!bRet)
        {
            goto Failure_label;
        }
        DWORD dwStatusCode;
        bRet = pFile->QueryInfoStatusCode(dwStatusCode);
        if (HTTP_STATUS_OK != dwStatusCode)//#define HTTP_STATUS_OK  200
        {
            CString strErrInfo;
            pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS, strErrInfo);
            goto Failure_label;
        }
        std::string sReceive;
        char buffer[1024] = { 0 };
        for (UINT len = 0; (len = pFile->Read(buffer, sizeof(buffer))) > 0; memset(buffer, 0, sizeof(buffer)))
        {
            std::string s(buffer, len);
            sReceive += s;
        }
        strReceive = CStringA(sReceive.c_str(), sReceive.length());//注意:若sReceive中有\0,可能会截断
        return true;
    }
    catch (CInternetException& e)
    {
        TCHAR szErr[512] = { 0 };
        e.GetErrorMessage(szErr, sizeof(szErr));
        CString strErrInfo;
        pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS, strErrInfo);
        goto Failure_label;
    }

Failure_label:
    if (pConn)
    {
        pConn->Close();
        delete pConn;
        pConn = NULL;
    }
    if (pFile)
    {
        pFile->Close();
        delete pFile;
        pFile = NULL;
    }
    return false;
}
BOOL CtestMFCHttpApp::InitInstance()
{
    CString url("https://www.cnblogs.com/12Zender/p/7596730.html");
    CString strReceive;
    BYTE* data = (BYTE*)"123";
    testHttp(url,data,3,strReceive);
}
View Code

相关文章: