title: Winhttp Does Not Post File
body: <p>I have combed all over here for similar questions and have done all suggestions but for some weird reason my code doesn't upload the file to the server.</p>
  <p>C++ Code</p>
  <pre><code>#include &lt;Windows.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;winhttp.h&gt;
  #include &lt;shlwapi.h&gt;
  
  #pragma comment(lib, &quot;Winhttp.lib&quot;)
  #pragma comment(lib, &quot;Shlwapi.lib&quot;)
  
  
  PCHAR sendWebRequest(LPWSTR szServerNameOrIP, LPWSTR szVerb, LPWSTR szServerPath, PCHAR szFileToPost)
  {
   HINTERNET hSession;
   HINTERNET hConnect;
   HINTERNET hRequest;
  
   BOOL bResults = FALSE;
  
   LPCWSTR szUserAgent = L&quot;MyCustomUserAgent&quot;;
  
   DWORD dwSize, dwDownloaded;
   PCHAR szTempRespBuff;
   PCHAR szRawResponse = (PCHAR)malloc(MAX_PATH); 
  
   strcpy_s(szRawResponse, MAX_PATH, &quot;&quot;);
  
   int nBuffSize;
  
  try{
  
      // Use WinHttpOpen to obtain a session handle.
      hSession = WinHttpOpen(szUserAgent, WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
      if (hSession == NULL)
      {
          //printf(&quot;[x] Could not create session!\n&quot;);
          MessageBox(0, L&quot;WinOpenError&quot;, L&quot;&quot;, 0);
          throw(0);
      }
  
      // Specify an HTTP server.
      hConnect = WinHttpConnect(hSession, szServerNameOrIP, INTERNET_DEFAULT_HTTP_PORT, 0);
      if (hConnect == NULL)
      {
          //printf(&quot;[x] Could not connect to the host!\n&quot;);
          MessageBox(0, L&quot;WinConnectError&quot;, L&quot;&quot;, 0);
          throw(0);
      }
  
      // Create an HTTP request handle.
      hRequest = WinHttpOpenRequest(hConnect, szVerb, szServerPath, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_REFRESH);
      if (hRequest == NULL)
      {
          //printf(&quot;[x] Failed to get request handle!\n&quot;);
          MessageBox(0, L&quot;WinOpenRequestError&quot;, L&quot;&quot;, 0);
          throw(0);
      }
  
      // Add request headers.
      bResults = WinHttpAddRequestHeaders(hRequest, 
                                          L&quot;Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryaUZw4AF9WYYzUrpM&quot;,
                                          -1L, WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_REPLACE);
  
      if (!bResults)
      {
          MessageBox(0, L&quot;WinHttpAddRequestHeadersError&quot;, L&quot;&quot;, 0);
          throw(0);
      }
  
      CHAR szPostData0[440],
           szPostData1[60],
           szPostTemp[120];
  
      ZeroMemory(szPostData0, sizeof(szPostData0));
      ZeroMemory(szPostData1, sizeof(szPostData1));
      ZeroMemory(szPostTemp, sizeof(szPostTemp));
  
  
      strcpy_s(szPostData0, 440, &quot;------WebKitFormBoundaryaUZw4AF9WYYzUrpM\r\n&quot;);
  
      PCHAR fileName  = PathFindFileNameA(szFileToPost);
      wsprintfA(szPostTemp, &quot;Content-Disposition: form-data; name=\&quot;fileToUpload\&quot;; filename=\&quot;%s\&quot;\r\n&quot;, fileName);
      strcat_s(szPostData0, 440, szPostTemp);
  
      strcat_s(szPostData0, 440, &quot;Content-Type: text/plain\r\n\r\n&quot;);
  
      strcpy_s(szPostData1, 60, &quot;\r\n------WebKitFormBoundarygQFYVyK9cnuuaQYb--\r\n&quot;);
  
       // Open the existing file.
      HANDLE hFile = CreateFileA(szFileToPost, GENERIC_READ, 0, NULL,
                           OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  
      if (hFile == INVALID_HANDLE_VALUE)
      {
          MessageBox(0, L&quot;CreateFileAError&quot;, L&quot;&quot;, 0);
          throw(0);
      }
  
      //Make sure size is not more than 300kb
      size_t fileSize = GetFileSize(hFile, NULL);
      if (fileSize &gt; 307200)
      {
          MessageBox(0, L&quot;FileSize too large!&quot;, L&quot;&quot;, 0);
          throw(0);
      }
  
  
      //Send Request
      int lenBytes = strlen(szPostData0 + 1) + strlen(szPostData1 + 1) + fileSize;
  
      bResults = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS , 0, WINHTTP_NO_REQUEST_DATA, 0, lenBytes, NULL);
      if (!bResults)
      {
          //printf(&quot;[x] Failed to send request!\n&quot;);
          int error = GetLastError();
  
          TCHAR szbuff[25];
          wsprintf(szbuff, L&quot;Error %d&quot;, error);
          MessageBox(0, L&quot;WinSendRequestError&quot;, szbuff, 0);
          throw(0);
      }
  
      //Write First Post Data
      DWORD numberOfBytesRead, numberOfBytesWritten;
      WinHttpWriteData(hRequest, szPostData0, (DWORD)strlen(szPostData0+1), &amp;numberOfBytesWritten);
  
      //Write File Buffer
      PCHAR fileBytes = (PCHAR)malloc(fileSize);
      ReadFile(hFile, fileBytes, fileSize, &amp;numberOfBytesRead, NULL);
      WinHttpWriteData(hRequest, fileBytes, fileSize, &amp;numberOfBytesWritten);
  
      
      //Write Last Post Data
      WinHttpWriteData(hRequest, szPostData1, (DWORD)strlen(szPostData1+1), &amp;numberOfBytesWritten);
  
  
      //Receive the response
      bResults = WinHttpReceiveResponse(hRequest, NULL);
      if (!bResults)
      {
          int err = GetLastError();
              wsprintfA(szPostTemp, &quot;WinHttpReceiveResponseError %d&quot;, err);
  
              MessageBoxA(0, szPostTemp, &quot;&quot;, 0);
          throw(0);
      }
  
      //Read Response Received
  
      do {
  
          // Check for available data.
          dwSize = 0;
          if( !WinHttpQueryDataAvailable(hRequest, &amp;dwSize) )
          {
              //printf( &quot;Error %u in WinHttpQueryDataAvailable.\n&quot;, GetLastError() );
              MessageBox(0, L&quot;WinHttpQueryDataAvailableError&quot;, L&quot;&quot;, 0);
              throw(0);
          }
  
          // Allocate space for the buffer.
          nBuffSize = (dwSize+1);
          szTempRespBuff = (PCHAR)malloc(nBuffSize);
  
          if (szTempRespBuff == NULL)
          {
              //printf( &quot;Error allocation space for response&quot;);
              throw(0);
          }
  
          // Read the data.
          ZeroMemory(szTempRespBuff, nBuffSize);
  
          if(WinHttpReadData( hRequest, (LPVOID)szTempRespBuff, dwSize, &amp;dwDownloaded ))
              strcat_s(szRawResponse, MAX_PATH, szTempRespBuff);
  
          // Free the memory allocated to the buffer.
          free(szTempRespBuff);
  
      }while (dwSize &gt; 0);
  
  
      //Close Handles
      WinHttpCloseHandle(hSession);
      WinHttpCloseHandle(hConnect);
      WinHttpCloseHandle(hRequest);
  
      return szRawResponse;
  
  }catch(...)
  {
      if(hSession)
          WinHttpCloseHandle(hSession);
  
      if(hConnect)
          WinHttpCloseHandle(hConnect);
  
      if(hRequest)
          WinHttpCloseHandle(hRequest);
  }
  
  
    return &quot;WINHTTP_ERROR&quot;;
  
  }
  
    int main()
  {
      PCHAR response = sendWebRequest(L&quot;127.0.0.1&quot;, L&quot;POST&quot;, L&quot;/panel/ups.php&quot;, &quot;hello.txt&quot;);
  
      MessageBoxA(0, response, &quot;&quot;, 0);
  
      return 0;
  }
  </code></pre>
  <p>PHP Code</p>
  <pre><code>&lt;?php
     if(isset($_FILES['fileToUpload'])){
        $errors= array();
        $file_name = $_FILES['fileToUpload']['name'];
        $file_size =$_FILES['fileToUpload']['size'];
        $file_tmp =$_FILES['fileToUpload']['tmp_name'];
        $file_type=$_FILES['fileToUpload']['type'];
        $file_ext = explode('.',$_FILES['fileToUpload']['name']);
        $file_ext=strtolower(end($file_ext));
        
        $extensions= array(&quot;jpeg&quot;,&quot;jpg&quot;,&quot;png&quot;, &quot;txt&quot;);
        
        if(in_array($file_ext,$extensions)=== false){
           $errors[]=&quot;extension not allowed, please choose a JPEG or PNG file or Txt&quot;;
        }
        
        if($file_size &gt; 1048576){
           $errors[]='File size must not exceed 1 MB';
        }
        
        if(empty($errors)==true){
           move_uploaded_file($file_tmp,&quot;images/&quot;.$file_name);
           echo &quot;success&quot;;
        }else{
           print_r($errors);
        }
     }
  ?&gt;
  &lt;html&gt;
     &lt;body&gt;
        
        &lt;form action=&quot;&quot; method=&quot;POST&quot; enctype=&quot;multipart/form-data&quot;&gt;
           &lt;input type=&quot;file&quot; name=&quot;fileToUpload&quot; /&gt;
           &lt;input type=&quot;submit&quot;/&gt;
        &lt;/form&gt;
        
     &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>What am I doing wrong? I get the &quot;success&quot; echo on server (php) but no file is uploaded into the images directory. I found out that the file has no <code>$_FILES['fileToUpload']['tmp_name']</code> and <code>$_FILES['fileToUpload']['size']</code> is 0 bytes.</p>