How to use Wininet to upload a file via HTTP and check the response

Posted on 2007-10-19
Last Modified: 2013-11-20
Dear Experts,
I would like to upload a file using Wininet and it actually seems to work.
I would like to see the response of my PHP script which is supposed to receive the file.
This doesn't work in my code below.

While at it, how can I specify the variable name for the receiving script?

bool CTask::UploadFile(HINTERNET hINet, HINTERNET hCon, const char *filePath) {
     INTERNET_BUFFERS BufferIn = {0};
     DWORD dwBytesRead;
     DWORD dwBytesWritten;
     BYTE pBuffer[1024]; // Read from file in 1K chunks
     BOOL bRead, bRet;

     BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS );

     HINTERNET hRequest = HttpOpenRequest (hCon, "PUT",
         "/2DB/admin/upAMS.php", NULL, NULL, NULL,  0, 0);
     if (!hRequest)
     HANDLE hFile = CreateFile (filePath, GENERIC_READ, FILE_SHARE_READ,
     if (hFile == INVALID_HANDLE_VALUE)
     BufferIn.dwBufferTotal = GetFileSize (hFile, NULL);
     if(!HttpSendRequestEx( hRequest, &BufferIn, NULL, HSR_INITIATE, 0))
     DWORD sum = 0;
       if  (!(bRead = ReadFile (hFile, pBuffer, sizeof(pBuffer),
           &dwBytesRead, NULL)))
       if (!(bRet=InternetWriteFile( hRequest, pBuffer, dwBytesRead,
       sum += dwBytesWritten;
     while (dwBytesRead == sizeof(pBuffer)) ;

// The below just won't work for me
// What I want is to get the server response after internetwritefile finishes (which seems to work fine).

      CString resp;
      char pcBuffer[500];
      printf("\nThe following was returned by the server:\n");
      {      dwBytesRead=0;
            if(InternetReadFile(hFile, pcBuffer, 499, &dwBytesRead))
                  pcBuffer[dwBytesRead]=0x00; // Null-terminate buffer
                  resp += pcBuffer;
                  printf("\nInternetReadFile failed");

     CloseHandle (hFile);
     if(!HttpEndRequest(hRequest, NULL, 0, 0))

I have been trying this for a while and it just won't work.
Question by:allmer
    LVL 11

    Expert Comment

    This one will give you both upload and download progress
    There is a good sample as well.

    Best Regards,
    LVL 5

    Author Comment

    I would appreciate if you could point out my mistake.
    LVL 11

    Accepted Solution

    Well you need to use somithing like,
    InternetStatusCallback() to know the status of your file writing. It will not work in your code since you have called InternetReadFile sequencially.
    Best Regards,
    LVL 11

    Expert Comment

    You can go through this as well.
    Hope this is what you are looking for.
    Best Regards,

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Suggested Solutions

    In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
    If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now