I want to upload "C:\test.txt" to webserver, when I am running program, file is not uploading and I am not getting any error.
the complete C++ code can be find here
and php code on webserver can be find here: "http://student114.110mb.com/upload.txt" or "http://student114.110mb.com/upload.php"
kindly help me where I am doing wrong
#include <windows.h>
#include <wininet.h>
#include <tchar.h>
#include <iostream>
#pragma comment(lib,"wininet.lib")
using namespace std;
int main()
{
static TCHAR frmdata[] = "-----------------------------7d82751e2bc0858\nContent-Disposition: form-data; name=\"uploadedfile\"; filename=\"C:\test.txt\"\nContent-Type: text/plain\n\nfile contents here\n-----------------------------7d82751e2bc0858--";
static TCHAR hdrs[] = "Content-Type: multipart/form-data; boundary=---------------------------7d82751e2bc0858";
HINTERNET hSession = InternetOpen("MyAgent",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if(hSession==NULL)
{
cout<<"Error: InternetOpen";
}
HINTERNET hConnect = InternetConnect(hSession, _T("localhost"),INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
if(hConnect==NULL)
{
cout<<"Error: InternetConnect";
}
HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST",_T("upload.php"), NULL, NULL, (const char**)"*/*\0", 0, 1);
if(hRequest==NULL)
{
cout<<"Error: HttpOpenRequest";
}
BOOL sent= HttpSendRequest(hRequest, hdrs, strlen(hdrs), frmdata, strlen(frmdata));
if(!sent)
{
cout<<"Error: HttpSendRequest";
}
//close any valid internet-handles
InternetCloseHandle(hSession);
InternetCloseHandle(hConnect);
InternetCloseHandle(hRequest);
return 0;
}
I was able to make your code work.
First of all the code on the link you provided and the code you posted is not the same:
InternetConnect(hSession, _T("localhost"), ...
InternetConnect(hSession, _T("http://student114.110mb.com"), ...
You must pass an host name or ip address here so "localhost" is good but "http://student114.110mb.com" isn't. If you pass an URL you will get the 12005 error code [see WinINet error codes on msdn].
Another problem is the frmdata string. You should double the backslash in C:\test.txt or you will get a tab character \t in your string. The \n before and after the delimiters should also be replaced by \r\n because RFC 1521 and most other internet protocols use CRLF as a line delimiter.
Here is the string I have used.
static TCHAR frmdata[] = "-----------------------------7d82751e2bc0858\r\nContent-Disposition: form-data; name=\"uploadedfile\"; filename=\"C:\\test.txt\"\r\nContent-Type: text/plain\r\n\r\nfile contents here\r\n-----------------------------7d82751e2bc0858--\r\n";
Finally the PHP code doesn't work because you use $_FILES["file"] where you should be using $_FILES["uploadedfile"]. "uploadedfile" would typically correspond to the name of an <input type="file"> tag in HTML but in your case it is specified in the name= parameter of the frmdata[] string.
Here's the PHP code I have used to test this
move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], "/files/my_file");
When you work on complex client/server interaction like this it helps to test each part separately. You could for instance.
Write a simple HTML upload form to test your php script
Have your program send its request to netcat and examine the output