Solved

CHttpConnection and https

Posted on 2004-08-12
5
2,408 Views
Last Modified: 2013-11-20
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
Comment
Question by:KurtVon
  • 3
  • 2
5 Comments
 
LVL 5

Accepted Solution

by:
millsoft earned 250 total points
ID: 11788919
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
 
LVL 5

Expert Comment

by:millsoft
ID: 11788923
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
 
LVL 5

Expert Comment

by:millsoft
ID: 11788925
correction:

pFile = pConnection->OpenRequest( GetVerb(), m_locale.PostObject(), NULL, 1, NULL, NULL, dwRequestFlags );
0
 
LVL 11

Author Comment

by:KurtVon
ID: 11797077
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
 
LVL 11

Author Comment

by:KurtVon
ID: 11797086
Oh yeah, and the port number is specified in the GetHttpConnection.  I just used INTERNET_DEFAULT_HTTPS_PORT.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question