0

I'm going to upload data using wininet in c++.
Here is a code.

        #include "stdafx.h"
    #include "iostream"
    #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;

        }

I am using visual studio 2012 for compiling it.
I also compile it in dev c++ but the error is same.

1>asdfg.obj : error LNK2019: unresolved external symbol __imp__InternetOpenW@20 referenced in function _main

1>asdfg.obj : error LNK2019: unresolved external symbol __imp__InternetConnectW@32 referenced in function _main
1>asdfg.obj : error LNK2019: unresolved external symbol __imp__HttpOpenRequestW@32 referenced in function _main
1>asdfg.obj : error LNK2019: unresolved external symbol __imp__HttpSendRequestW@20 referenced in function _main

UPDATE
I'm using #pragma comment(lib, "Wininet.lib") also and getting no error.
all the program compile successfully but the data is not upload.
here is php file.

<?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);

   ?>
Community
  • 1
  • 1
Axeem
  • 193
  • 1
  • 3
  • 16

1 Answers1

0

You need to link against the correct library, for <wininet.h> it is Wininet.lib.

Also, when you include system headers like wininet.h or sstream.h, you should enclose their names in < > rather than " ", which are for local project files.

Kelm
  • 967
  • 5
  • 14