CHttpFile problem accessing HTTPS from ATL

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Nedelcho Stanev (decho_at_atlanticsky.com)
Date: 03/04/04


Date: 4 Mar 2004 10:18:24 -0800

Hello All,

I have COM component - sink for exchange.
And i want to access from this component some secure web site
to post data.
And i tried with CHttpFile from MFC , used from me
in lot of projects.
What was my surprise when i understand that this simply doesent
works.
Or no, it works, but, with very strange behavior.
When i set flags to ignore wrong CA certificates - it doesent work
simply
and i get error:
"The certificate authority is invalid or incorrect"
This is confusing for me because same code in standalone exe works
very fine.
Then i extract this code and put in separated dll.
Result is same, from standalone exe , linked with this dll it works
perfect, from COM object - never.
So , for me is curious what is this strange behavior, is it known bug
?
or problems are in mine code?
and second strange problem is that , ok i disable security and try to
access normal web site on port 80. Then i see in logs of server that
whatever i put as method it issue "GET" to the server and never "POST"
Can someone help with resolving of this?

Below is code for function which i use in dll:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
extern "C" DWORD PASCAL EXPORT GetWebFile2(std::string& pMem, CString
strData, bool bSecure)
{
        AFX_MANAGE_STATE(AfxGetStaticModuleState());

        DWORD dwFlags = 0;
        int icntread = 0;
        int numread = 0;
    CHttpFile* file = NULL;
    CHttpConnection* server = NULL;
        TCHAR szErr[COMMON_BUFSIZE];

        szErr[0] = '\0';
        DWORD dwSessionFlags;
        if( bSecure )
                dwSessionFlags = INTERNET_FLAG_EXISTING_CONNECT
| INTERNET_FLAG_NO_AUTO_REDIRECT |
                                INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE |
                                INTERNET_FLAG_SECURE | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID |
INTERNET_FLAG_IGNORE_CERT_CN_INVALID ;
        else
                dwSessionFlags = INTERNET_FLAG_EXISTING_CONNECT
| INTERNET_FLAG_NO_AUTO_REDIRECT |
                                INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE ;

    CInternetSession session(HTTPS_SESSION_NAME);

    try {
                        if( bSecure )
                                server=session.GetHttpConnection(HTTPS_LOCALHOST,HTTPS_PORT,MQUBE_RPCUSER,MQUBE_RPCPASSWORD);
                        else
                                server=session.GetHttpConnection(HTTPS_LOCALHOST,HTTP_PORT,MQUBE_RPCUSER,MQUBE_RPCPASSWORD);
                        file=server->OpenRequest(
                                        CHttpConnection::HTTP_VERB_POST,
                                        HTTPS_REQ_BASE,
                                        NULL,
                                        1,
                                        NULL,
                                        NULL,
                                        dwSessionFlags );

    } catch (CInternetException* pEx) {
                        if(!pEx->GetErrorMessage(szErr, COMMON_BUFSIZE))
                                strcpy(szErr,"Unknown error.");
                        LogLineToLogServer(MQ_LOG_DEBUG,"[80500] Cannot initialize
session:%s.\n",szErr);
            file=NULL;
            pEx->Delete();
                        return -1;
    }
    if (server==NULL)
                return -2;

    if (file!=NULL) {
                if( bSecure ) {
                        // disable CA check
                        file->QueryOption( INTERNET_OPTION_SECURITY_FLAGS, dwFlags );
                        dwFlags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;
                        file->SetOption( INTERNET_OPTION_SECURITY_FLAGS, dwFlags );
                }
                // send header
                file->AddRequestHeaders(HTTPS_HEADER);
                try {
                        // finaly - send request
                        file->SendRequest(NULL,0,(void
*)(LPCTSTR)strData,strData.GetLength());
                } catch (CInternetException* pEx) {
                                if(!pEx->GetErrorMessage(szErr, COMMON_BUFSIZE))
                                        strcpy(szErr,"Some crazy unknown error");
                                LogLineToLogServer(MQ_LOG_DEBUG,"[80501] Cannot send
request:%s.\n",szErr);
                                file=NULL;
                                server->Close();
                                delete server;
                                pEx->Delete();
                                return -3;
                }
 
                // and process response
                while ((icntread = file->Read(szErr,COMMON_BUFSIZE)) > 0)
                {
                        numread += icntread;
                        pMem.append(szErr,icntread);
                        memset(szErr,0x00,COMMON_BUFSIZE);
                }

                file->Close();
        delete file;
        }
    server->Close();
    delete server;
        return numread;
}



Relevant Pages

  • class is defined in multiple places
    ... the class is not defined twice. ... Its a web site (the main dll) that runs ... I've been all over the server and deleted every other occurrance of ...
    (microsoft.public.dotnet.general)
  • Re: Cant see performance report, etc. SBS2003
    ... However, for "OpenSSH Server", you need to ensure it started, or set ... click to check the "Hide All Microsoft Services" ... Make sure no host header is assigned to the Default Web Site. ... Reinstall Monitoring component: ...
    (microsoft.public.windows.server.sbs)
  • Re: IIS (on SBS 2k) default website stopped address already in use
    ... server box properly and configure the following settings on the ISA server. ... Create an anonymous Site and Content rule for Windows Update ... Web site through a server that is running ISA Server ... Microsoft CSS Online Newsgroup Support ...
    (microsoft.public.windows.server.sbs)
  • Re: publishing multiple websites in ISA 2004
    ... hosted off of a second server that is a member server in the network. ... We can publish multiple internal websites through ISA ... INSIDE OF SBS NETWORK, IS SAFER THAN JUST ON THE SBS/ISA BOX, CORRECT? ... 816576 How to create a new virtual server or Web site in Internet ...
    (microsoft.public.windows.server.sbs)
  • Re: Netopia 3347NWG with Remote Desktop and Remote Web Workplace
    ... Microsoft Exchange Best Practices Analyzer Web Update Pack ... Windows Backup Wizard has not yet run [I know -- I was waiting to get this ... Microsoft Windows Small Business Server 2003 Best Practices Analyzer ... I configured the server extensions for the Default Web Site under sbs2003, ...
    (microsoft.public.windows.server.sbs)