使用CInternetSession CHttpConnection CHttpFile进行HTTPS站点连接

时间:2019-06-11 13:04:46

标签: c++ https

我不熟悉HTTPS连接(原生MFC / C ++环境)(VisualStudio 2019) 我的主题是创建代码以连接到服务提供商以发送一个或多个SMS。 (提供商页面:https://developers.esendex.com/api-reference#messagedispatcher) 下面的代码在“ pFile-> QueryInfo”上返回“错误400” 我不知道问题出在哪里... 你能帮我吗?

CString strData="<messages><accountreference>moncompte</accountreference><message><to>0611223344</to><body>texte du SMS</body></message></messages>";
CInternetSession session("SMS");
CHttpConnection *pSession=NULL;
CHttpFile *pFile=NULL;
int sockFail=0;
try
{Session=session.GetHttpConnection("api.esendex.com",INTERNET_FLAG_SECURE,443,NULL,NULL);
}
catch(CInternetException *pEx)
{   TCHAR sz[1024];
    pEx->GetErrorMessage(sz,1024);
        AfxMessageBox(sz,MB_ICONEXCLAMATION|MB_OK|MB_DEFBUTTON1);
    pEx->Delete();
}

if(pSession)
{   CString header;
    header="Content-Type: application/xml\r\n";
    header+="<?xml version='1.0' encoding='UTF-8'?>\r\n";
    header+="Authorization: Basic ";
    //{
    //    char szBuffer[1024];
    //    CString strTmp;
    //    strTmp.Format("%s:%s",
    //                  static_cast<LPCTSTR>(theApp.m_strEsendexUser),
    //                  static_cast<LPCTSTR>(theApp.m_strEsendexPassword));
    //    StrToBase64(strTmp,szBuffer);
    //    header+=szBuffer;
    //}
    header+="user + mdp en base 64 (vérifié: le pb ne semble pas être ici)";
    header+="\r\n";
    try
    {    DWORD dwFlags=INTERNET_FLAG_SECURE|INTERNET_FLAG_IGNORE_CERT_DATE_INVALID|INTERNET_FLAG_IGNORE_CERT_CN_INVALID;
        pFile=pSession->OpenRequest(CHttpConnection::HTTP_VERB_POST,"https://api.esendex.com/v1.0/messagedispatcher",/*pstrReferer*/0,/*dwContext*/1,/*ppstrAcceptTypes*/0,"HTTP/1.1",dwFlags);
    }
    catch(CInternetException *pEx)
    {   TCHAR sz[1024];
        pEx->GetErrorMessage(sz,1024);
        AfxMessageBox(sz,MB_ICONEXCLAMATION|MB_OK|MB_DEFBUTTON1);
        pEx->Delete();
    }
}
if(pFile)
{
    try
    {   pFile->AddRequestHeaders(header);
    }
    catch(CInternetException *pEx)
    {   TCHAR sz[1024];
        pEx->GetErrorMessage(sz,1024);
        AfxMessageBox(sz,MB_ICONEXCLAMATION|MB_OK|MB_DEFBUTTON1);
        pEx->Delete();
    }
    try
    {   pFile->SendRequestEx(strData.GetLength());
        pFile->WriteString(strData);
        pFile->EndRequest();
    }
    catch(CInternetException *pEx)
    {   TCHAR sz[1024];
        pEx->GetErrorMessage(sz,1024);                    // here, sz="The certification authority is not valid or correct"
        AfxMessageBox(sz,MB_ICONEXCLAMATION|MB_OK|MB_DEFBUTTON1);
        pEx->Delete();
    }
    CString retHeader;
    pFile->QueryInfo(HTTP_QUERY_STATUS_CODE,retHeader,0);
    if(retHeader!="200")
    {   sockFail=1;
    }
    else
    {
        CString buff;
        while(pFile->ReadString(buff))
        {   strResponse+=buff;
            strResponse+="\n";
            buff.Empty();
        }
    }
    pFile->Close();
    delete pFile;
    pSession->Close();
    delete pSession;
}

0 个答案:

没有答案