C++ HTTP request method (MFC version)

#include "afxinet.h"

int HttpRequest(const CString & method, const CString & url, const CString& strHeader, const CString & sendData, CString & recvData, const CString & strAgent)
{
    
    
  CString strServer;
  CString strObject;
  DWORD dwServiceType;
  INTERNET_PORT nPort = 80;

  //先解析一下url
  BOOL bParseUrl = AfxParseURL(url.GetString(), dwServiceType, strServer, strObject, nPort);

  if (AFX_INET_SERVICE_HTTP != dwServiceType && AFX_INET_SERVICE_HTTPS != dwServiceType)
  {
    
    
    return HTTP_FAILURE;
  }

  //CInternetSession *pSession    = new CInternetSession(strAgent.c_str());
  CInternetSession *pSession = new CInternetSession(NULL, 0);
  CHttpConnection  *pConnection = NULL;
  CHttpFile        *pHttpFile = NULL;

  try
  {
    
    
    //创建一个http链接
    pConnection = pSession->GetHttpConnection(strServer,
      dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_CONNECT : SECURE_CONNECT,
      nPort);
    pConnection->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 2000);			// 5秒的连接超时
    pConnection->SetOption(INTERNET_OPTION_SEND_TIMEOUT, 1000);				// 1秒的发送超时
    pConnection->SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT, 5000);			// 7秒的接收超时
    pConnection->SetOption(INTERNET_OPTION_DATA_SEND_TIMEOUT, 1000);		// 1秒的发送超时
    pConnection->SetOption(INTERNET_OPTION_DATA_RECEIVE_TIMEOUT, 5000);		// 7秒的接收超时
    pConnection->SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);				// 1次重试
    //开始一个http请求,映射成HttpFile
    pHttpFile = pConnection->OpenRequest(method.GetString(), strObject,
      NULL, 1, NULL, NULL,
      (dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_REQUEST : SECURE_REQUEST));

    //DWORD dwFlags;
    //m_pFile->QueryOption(INTERNET_OPTION_SECURITY_FLAGS, dwFlags);
    //dwFlags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;
    //set web server option
    //m_pFile->SetOption(INTERNET_OPTION_SECURITY_FLAGS, dwFlags);

    //pHttpFile->AddRequestHeaders(_T("Accept: *,* / *"));
    //pHttpFile->AddRequestHeaders("Accept-Language: zh-cn");
    //pHttpFile->AddRequestHeaders("Content-Type: application/x-www-form-urlencoded");
    //pHttpFile->AddRequestHeaders("Accept-Encoding: gzip, deflate");
    //pHttpFile->AddRequestHeaders("\'User-Agent\':\'Mozolla/5.0\' (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36");
    //pHttpFile->AddRequestHeaders(header.GetString());

    pHttpFile->AddRequestHeaders(strHeader);
    USES_CONVERSION;
    wchar_t *pwideBuf = nullptr;
    char *pmultibuf = nullptr;

    int nwidebuflen = MultiByteToWideChar(CP_ACP, 0, W2A(sendData), -1, NULL, 0);
    pwideBuf = new wchar_t[nwidebuflen + 1];
    memset(pwideBuf, 0, (nwidebuflen + 1) * sizeof(wchar_t));
    MultiByteToWideChar(CP_ACP, 0, W2A(sendData), -1, pwideBuf, nwidebuflen);


    int nmutibuflen = WideCharToMultiByte(CP_UTF8, 0, pwideBuf, -1, NULL, 0, NULL, NULL);
    pmultibuf = new char[nmutibuflen + 1];
    memset(pmultibuf, 0, nmutibuflen + 1);
    WideCharToMultiByte(CP_UTF8, 0, pwideBuf, -1, pmultibuf, nmutibuflen, NULL, NULL);

    DWORD dwOptionalLen = strlen(pmultibuf);
    pHttpFile->SendRequest(NULL, 0, (LPVOID)pmultibuf, dwOptionalLen);
    //发送请求
    //pHttpFile->SendRequest(NULL, 0, (LPVOID)sendData.GetString(), sendData.GetLength());

    char szChars[1024] = {
    
     0 };
    std::string strRawResponse;
    UINT nReaded = 0;
    while ((nReaded = pHttpFile->Read((void*)szChars, 1024)) > 0)
    {
    
    
      strRawResponse.append(szChars, nReaded);
    }
    recvData = CString(strRawResponse.c_str());

    int nBufferSize = MultiByteToWideChar(CP_UTF8, 0, strRawResponse.c_str(), -1, NULL, 0); //取得所需缓存的多少
    wchar_t *pBuffer = (wchar_t*)malloc(nBufferSize * sizeof(wchar_t));//申请缓存空间
    MultiByteToWideChar(CP_UTF8, 0, strRawResponse.c_str(), -1, pBuffer, nBufferSize * sizeof(wchar_t));//转码
    recvData = CString(pBuffer);
    //CString recvStr;
    //while (pHttpFile->ReadString(recvStr) > 0)
    //{
    
    
    //	recvData += recvStr;
    //}
    //std::string st = CT2A(recvData);
    if (NULL != pHttpFile)
    {
    
    
      pHttpFile->Close();
      delete pHttpFile;
      pHttpFile = NULL;
    }
    if (NULL != pConnection)
    {
    
    
      pConnection->Close();
      delete pConnection;
      pConnection = NULL;
    }
    if (NULL != pSession)
    {
    
    
      pSession->Close();
      delete pSession;
      pSession = NULL;
    }
  }
  catch (CInternetException* e)
  {
    
    
    if (NULL != pHttpFile)
    {
    
    
      pHttpFile->Close();
      delete pHttpFile;
      pHttpFile = NULL;
    }
    if (NULL != pConnection)
    {
    
    
      pConnection->Close();
      delete pConnection;
      pConnection = NULL;
    }
    if (NULL != pSession)
    {
    
    
      pSession->Close();
      delete pSession;
      pSession = NULL;
    }


    DWORD dwErrorCode = e->m_dwError;
    e->Delete();

    DWORD dwError = GetLastError();

    if (ERROR_INTERNET_TIMEOUT == dwErrorCode)
    {
    
    
      return HTTP_OUTTIME;
    }
    else
    {
    
    
      return HTTP_FAILURE;
    }
  }
  return HTTP_SUCCESS;
}

Guess you like

Origin blog.csdn.net/Liang_ming_/article/details/131665060