title: POST form data using WinInet c++
body: <p>I'm trying to make this program connect to a website and submit form data in order to login, but I don't know what I'm doing wrong. I have heard of others like curl and Winsock but I chose the WinINet library. So just for the testing of this program I've been using the website Pastebin to post to. So far, I haven't seen any results from this. If this program succeeds in posting the form data it will give me the header to the location of the post on their site.</p>
  
  <p>Am I writing the form data char* correctly? I have seen on other stackoverflow posts where they had a large amount of dashes before some number then the put their form data.  </p>
  
  <p>Do I need to add something to it make it simulate clicking the submit button?<br>
  Do I need to write out values for each elements on the form?</p>
  
  <p>I have tried HttpAddRequestHeaders and that didn't help me.</p>
  
  <p>Also, I get the ERROR_INSUFFICIENT_BUFFER error on HttpOpenRequest but it still returns a valid HINTERNET.</p>
  
  <pre><code>#include &lt;Windows.h&gt;
  #include &lt;WinInet.h&gt;
  #include &lt;iostream&gt;
  
  #pragma comment( lib,"Wininet.lib")
  using namespace std;
  
  char* getheaders(HINTERNET hRequest){
      DWORD dwInfoLevel=HTTP_QUERY_RAW_HEADERS_CRLF;
      DWORD dwInfoBufferLength=10;
      char* pInfoBuffer=(char*)malloc(dwInfoBufferLength+1);
      while(!HttpQueryInfo(hRequest,dwInfoLevel,pInfoBuffer,&amp;dwInfoBufferLength,NULL)){
          if (GetLastError()==ERROR_INSUFFICIENT_BUFFER){
              free(pInfoBuffer);
              pInfoBuffer=(char*)malloc(dwInfoBufferLength+1);
          }else{
              fprintf(stderr,"HttpQueryInfo failed, error = %d (0x%x)\n",GetLastError(),GetLastError());
              break;
          }
      }
      pInfoBuffer[dwInfoBufferLength] = '\0';
      return pInfoBuffer;
  }
  
  void readfile(HINTERNET hRequest,char** buffs,int size){
      DWORD dwBytesAvailable;
      DWORD dwBytesRead;
      for(int i=0;i&lt;size;i++){
          if(!InternetQueryDataAvailable(hRequest,&amp;dwBytesAvailable,0,0)) break;
          buffs[i]=(char*)malloc(dwBytesAvailable+1);
          bool bResult=InternetReadFile(hRequest,buffs[i],dwBytesAvailable,&amp;dwBytesRead);
          if(!bResult | dwBytesRead==0) break;
      }
  }
  
  
  int main(int argc,char** argv){
      char* hdrs="Content-Type: application/x-www-form-urlencoded";
      char* frmdata="paste_code=test";
      LPCSTR accept[2]={"*/*", NULL};
  
      HINTERNET hSession = InternetOpen("http generic",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
      HINTERNET hConnect = InternetConnect(hSession, "www.pastebin.com",INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
      HINTERNET hRequest = HttpOpenRequest(hConnect, "GET","/", NULL, NULL, accept, 0, 0);
      //ERROR_INSUFFICIENT_BUFFER (122) with "accept".
  
      bool send=HttpSendRequest(hRequest, hdrs, strlen(hdrs), NULL,NULL);
      if(!send){
          printf("HttpSendRequest failed, code=%d",GetLastError());
          system("pause&gt;nul");
          return 0;
      }
  
      char* heads=getheaders(hRequest);
      printf("%s\n\n\n\n",heads);
  
      HINTERNET hRequest2 = HttpOpenRequest(hConnect, "POST","/", NULL, NULL, accept, 0, 0);
      //ERROR_INSUFFICIENT_BUFFER (122) with "accept".
  
      send=HttpSendRequest(hRequest2, hdrs, strlen(hdrs), frmdata,strlen(frmdata));
      if(!send){
          printf("HttpSendRequest failed, code=%d",GetLastError());
          system("pause&gt;nul");
          return 0;
      }
  
      heads=getheaders(hRequest);
      printf("%s\n\n\n\n",heads);
  
      InternetCloseHandle(hRequest);
      InternetCloseHandle(hRequest2);
      InternetCloseHandle(hConnect);
      InternetCloseHandle(hSession);
  
      system("pause&gt;nul");
      return 0;
  }
  </code></pre>