InetHttpSendData 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_DATA

HTTP Data

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_DATA.

 

d.

In the Messages list box, select BN_CLICKED.

 

e.

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

 

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::OnHttpData()
{
   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_POST, "/com/tutorial/http/upload.asp", "", "HTTP/1.0");
      if(nRet == 0)
      {
         long nStatus;
         INAMEVALUEItem* nv=NULL;
         CoCreateInstance(CLSID_NAMEVALUEItem, NULL, CLSCTX_ALL, IID_INAMEVALUEItem, (void**)&nv);
         if(!nv)
         {
            AfxMessageBox(TEXT("Error creating Name/Value item"));
            m_pRasterHTTP->InetHttpCloseRequest();
            m_pRasterHTTP->InetHttpDisconnect();
            return;
         }
         nv->pszName = TEXT("Data");
         nv->pszValue = TEXT("data1.raw");
         //Fill it with data
         for(int i=0; i<1000; ++i)
            pltRasVar->ShortItemValue [i] = 65 + i / 10;
         //Unlock the variant data

         nRet = m_pRasterHTTP->InetHttpSendData(pltRasVar, 1000, "image/jpg", nv);

         nv->Release();
         if(nRet != 0)
         {
            AfxMessageBox(TEXT("Error Sending Data"));
            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 ();
   }
}

4.

Rebuild and run the application.