Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2834
  • Last Modified:

CHttpConnection and https

The Windows docs say that CHttpConnection does support https, but I haven't been able to figure out how and there are no examples I could find.  Here's the code I have:

std::string DoIt(const std::string& strServer, const std::string& strQuery)
{
    std::string strResult;
    CInternetSession *pSession;
    CHttpConnection *pConnection;
   
    pSession = new CInternetSession("test");
    pConnection = pSession->GetHttpConnection(strServer.c_str(),
                                              INTERNET_DEFAULT_HTTP_PORT,
                                              "", "");

    if (pConnection != NULL)
    {
        CHttpFile* pFile;

        pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET,
                                         strQuery.c_str(),
                                         NULL,
                                         1,
                                         NULL,
                                         NULL,
                                         INTERNET_FLAG_RELOAD);
        if (pFile != NULL)
        {
            DWORD dwStatus;

            pFile->SendRequest();
            pFile->QueryInfoStatusCode(dwStatus);

            if ((dwStatus >= 200) && (dwStatus < 300))
            {
                // Read the CHttpFile
            }
        }
    }

    delete pSession;

    return strResult;
}

A few chunks have been deleted, but all internet related calls are still there.  It works fine, I just need to make it use https instead of http.

Thanks.
0
KurtVon
Asked:
KurtVon
  • 3
  • 2
1 Solution
 
millsoftCommented:
You have to set some options on the OpenRequest:

DWORD dwRequestFlags = INTERNET_FLAG_SECURE | INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_NO_AUTO_REDIRECT;

pFile = pConnection->OpenRequest( GetVerb(), m_locale.PostObject(), NULL, 1, NULL, NULL, DWORD );
0
 
millsoftCommented:
Of course, you also have to open the session on port 443.

So strServer on your session create line needs to read "https://www.foo.com"
0
 
millsoftCommented:
correction:

pFile = pConnection->OpenRequest( GetVerb(), m_locale.PostObject(), NULL, 1, NULL, NULL, dwRequestFlags );
0
 
KurtVonAuthor Commented:
Sorry it took so long to respond.

I have it sort of working (sever error 500) but I had to drop the https://.  It took a while to confirm this was  a problem in general.  When I thought to test with http:// prepended to the server name and using the previously working non-secure method I got the same exception thrown, so I'm pretty sure that was it.

Since the server error 500 seems to show up under IE, it isn't surprising the MFC is doing it.  So I'll mark this as answered.  Thanks.
0
 
KurtVonAuthor Commented:
Oh yeah, and the port number is specified in the GetHttpConnection.  I just used INTERNET_DEFAULT_HTTPS_PORT.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now