Upload data using c++.

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.
remove the http:// and try it
And your frmdata need not be unicode
Content type is wrong:
Content-Type: application/x-www-form-urlencoded


Need to be multipart/form-data.

Also in your PHP script you need to use $_FILES array to access uploaded content.

I strongly advise you to use libcurl instead of wininet, because is much simpler and you already have some working example on their website.
http://curl.haxx.se/libcurl/
http://curl.haxx.se/libcurl/c/postit2.html
I am interested in this topic, thank the experts advise ...
http://source-projects.appspot.com/project/homemate.html
Last edited on
Here's an example and you can echo your form data and this will pick it up from the php page.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
static TCHAR hdrs[] =_T("Content-Type: application/x-www-form-urlencoded");
const char * const frmdata = "name=12&userid=someone&other=myothertext";
static LPCWSTR accept[] = {_T("*/*"), NULL};
DWORD dwBytesRead = 0;
DWORD dwRead = NULL;
DWORD dwContext = NULL;
HINTERNET hSession = InternetOpen(_T("MyAgent"),INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
HINTERNET hConnect = InternetConnect(hSession, _T("yoururl.com"),INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
HINTERNET hRequest = HttpOpenRequest(hConnect, _T("POST"),_T("/yourpage.php"), NULL, NULL, accept, 0, 1);
bool response = HttpSendRequest(hRequest, hdrs, _tcslen(hdrs), (LPVOID) frmdata, strlen(frmdata));

char pOutBuf[1024];
memset(&pOutBuf,0,sizeof(pOutBuf));
char* pTempBuf=pOutBuf;
while(InternetQueryDataAvailable(hRequest,&dwBytesRead,0,0) && dwBytesRead != 0)
{
InternetReadFile(hRequest,pTempBuf,dwBytesRead,&dwBytesRead);
pTempBuf += dwRead;
}
int err = GetLastError();
std::cout << response << pTempBuf << std::endl;

InternetCloseHandle(hSession);
InternetCloseHandle(hConnect);
InternetCloseHandle(hRequest);
system("pause");
return 0;
Last edited on
Topic archived. No new replies allowed.