Используйте CInternetSession CHttpConnection CHttpFile для подключения к HTTPS-сайту - PullRequest
0 голосов
/ 11 июня 2019

Я не знаком с HTTPS-соединениями (собственная среда MFC / C ++) (VisualStudio 2019) Моя тема - создать код для подключения к поставщику услуг для отправки одного или нескольких SMS. (страница провайдера: https://developers.esendex.com/api-reference#messagedispatcher) Код ниже возвращает меня «ошибка 400» на «pFile-> QueryInfo» Я не знаю, откуда проблема ... Вы можете мне помочь?

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;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...