InetHttpSendRequest example for C++ 5.0 and later

1.

Start with the project you created in InetHttpSendForm example for C++ 5.0 and later

2.

Add a command buttons to the Tutor dialog and name it as follows:

 

ID

Caption

 

IDC_HTTP_REQUEST

HTTP Request

3.

Press Ctrl-W to go to the MFC Class Wizard; then do the following:

 

a.

Click the Message Maps tab.

 

b.

In the Class Name combo box, select CTutorDlg.

 

c.

In the Object IDs list box, select IDC_HTTP_REQUEST.

 

d.

In the Messages list box, select BN_CLICKED.

 

e.

Click the Add function button. Choose OK for the default function name (OnHttpRequest).

 

f.

Click the Edit Code button and enter the following code: (you will have to change the IP address to a valid server name or address)

void CTutorDlg::OnHttpRequest()
{
   short nRet;
   ILEADRasterVariant * pltRasVar = NULL;

   CoCreateInstance(CLSID_LEADRasterVariant, NULL, CLSCTX_ALL,
IID_ILEADRasterVariant, (void **)&pltRasVar);

   nRet = m_pRasterHTTP->InetHttpConnect("demo.leadtools.com", 80, "", "");
   if(nRet == 0)
   {
      nRet = m_pRasterHTTP->InetHttpOpenRequest(HTTP_RQST_GET, "/com/default.htm", "", "HTTP/1.0");
      if(nRet == 0)
      {
         long nStatus;

         nRet = m_pRasterHTTP->InetHttpSendRequest(pltRasVar, 0, pltRasVar, 0);
         if(nRet != 0)
         {
            AfxMessageBox(TEXT("Error Sending request"));
            m_pRasterHTTP->InetHttpCloseRequest ();
            m_pRasterHTTP->InetHttpDisconnect ();
            return;
         }
         nStatus = m_pRasterHTTP->InetHttpGetServerStatus ();
         _bstr_t szResponse = m_pRasterHTTP->InetHttpGetResponse ();
         if(nStatus == HTTP_STATUS_OK)
         {
            FILE * fp = _tfopen(TEXT("c:\\output.htm"), TEXT("wb"));
            if(fp)
            {
               fwrite((TCHAR *)szResponse, szResponse.length(), 1, fp);
               fclose(fp);
            }
         }
         else
         {
            AfxMessageBox(TEXT("Problem With Server"));
            FILE * fp = _tfopen(TEXT("c:\\output.htm"), TEXT("wb"));
            if(fp)
            {
               fwrite((TCHAR *)szResponse, szResponse.length(), 1, fp);
               fclose(fp);
            }
         }
         m_pRasterHTTP->InetHttpCloseRequest ();
      }
      m_pRasterHTTP->InetHttpDisconnect ();
   }

   pltRasVar->Release();
}

4.

Rebuild and run the application.