Search code examples
c++httpuploadwininet

Is there any good example of http upload using WinInet c++ library


I cannot get my code to work :/


Solution

  • Here's a quick example from Microsoft.

       static TCHAR hdrs[] =
          _T("Content-Type: application/x-www-form-urlencoded");
       static TCHAR frmdata[] =
          _T("name=John+Doe&userid=hithere&other=P%26Q");
      static LPSTR accept[2]={"*/*", NULL};
    
       // for clarity, error-checking has been removed
       HINTERNET hSession = InternetOpen("MyAgent",
          INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
       HINTERNET hConnect = InternetConnect(hSession, _T("ServerNameHere"),
          INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
       HINTERNET hRequest = HttpOpenRequest(hConnect, "POST",
          _T("FormActionHere"), NULL, NULL, accept, 0, 1);
       HttpSendRequest(hRequest, hdrs, strlen(hdrs), frmdata, strlen(frmdata));
       // close any valid internet-handles
    

    The example comes from here.