Menu

Upload data using c++.

Compiling
Axeem
2015-03-28
2015-04-10
  • Axeem

    Axeem - 2015-03-28

    I am using this code for uploading a data in web but here is some error in this code.
    here is a code.

    #include <windows.h>
    #include <wininet.h>
    #include <tchar.h>
    #include <iostream>
    #include <string>
    #include <sstream>
    
    #pragma comment(lib, "ws2_32.lib")
    
    int main(){
    
            TCHAR hdrs[] = _T("Content-Type: application/x-www-form-urlencoded");
    TCHAR frmdata[] = _T("name=John+Doe&userid=hithere&other=P%26Q");
    LPCTSTR accept[] = {_T("*/*"), NULL};
    
    HINTERNET hSession = InternetOpen(_T("MyAgent"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
    HINTERNET hConnect = InternetConnect(hSession, _T("http://localhost"), INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
    HINTERNET hRequest = HttpOpenRequest(hConnect, _T("POST"), _T("/upload.php"), NULL, NULL, accept, 0, 1);
    HttpSendRequest(hRequest, hdrs, _tcslen(hdrs), frmdata, _tcslen(frmdata));
    
       return 0;
    
        }
    

    Here is a php code.

    <?php
    
     $name = $_POST['name'];
     $userid = $_POST['userid'];
     $other = $_POST['other'];
    
      $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
      $txt1 = $name."\n";
      fwrite($myfile, $txt1);
      $txt2 = $userid."\n";
      fwrite($myfile, $txt2);
      $txt3 = $other."\n";
      fwrite($myfile, $txt3);
      fclose($myfile);
    
     ?>
    

    Here is a [liker error]. I am using dev c++ for compiling that.

     

    Last edit: Axeem 2015-03-28
  • M.Trifon

    M.Trifon - 2015-04-10

    In order to get a more specific reply, you should have sent what actually [liker error] says...
    It appears your forgot to add to linker parameters one or two libraries. Retry after adding -lwininet and -lwinhttp to Project Parameters page, Linker section.
    (Side note re. your code: don't forget to close the handles once you finished !)

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.