Question

Dev C++ Wininet Upload file using HTTP here is the example with wininet. I've changed it, but i can't make it work! First of all there were warnings that char functions used with TCHAR varibales. I've replaced those functions with proper-ones.

#include <windows.h>
#include <wininet.h>
#include <tchar.h>
#include <iostream>

#include "stdafx.h"
#include <windows.h>
#include <wininet.h>
#include <tchar.h>
#include <iostream>

#pragma comment(lib,"wininet.lib")

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    static TCHAR frmdata[] = L"-----------------------------7d82751e2bc0858\r\nContent-Disposition: form-data; name=\"file\"; filename=\"C:\\test.txt\"\r\nContent-Type: text/plain\r\n\r\nAAAAAAAAAAAAAAAAA\r\n-----------------------------7d82751e2bc0858--\r\n";
    static TCHAR hdrs[] = L"Content-Type: multipart/form-data; boundary=---------------------------7d82751e2bc0858"; 

    HINTERNET hSession = InternetOpen(L"MyAgent",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
     if(hSession==NULL)
    {
     cout<<"Error: InternetOpen";  
    }


    HINTERNET hConnect = InternetConnect(hSession, L"localhost",INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
     if(hConnect==NULL)
    {
     cout<<"Error: InternetConnect";  
    }

    HINTERNET hRequest = HttpOpenRequest(hConnect, L"POST",L"/upload/upload.php", NULL, NULL, (LPCWSTR *)"*/*\0", 0, 1);
    if(hRequest==NULL)
    {
     cout<<"Error: HttpOpenRequest";  
    }

    BOOL sent= HttpSendRequest(hRequest, hdrs, wcslen(hdrs), frmdata, wcslen(frmdata));
    if(!sent)
    {
     cout<<"Error: HttpSendRequest";
     }

    //close any valid internet-handles
    InternetCloseHandle(hSession);
    InternetCloseHandle(hConnect);
    InternetCloseHandle(hRequest);
}

[/code]

But still it doesnt work correctly! Thats what i got with netcat

connect to [127.0.0.1] from validation.sls.microsoft.com [127.0.0.1] 52503
POST /upload/upload.php HTTP/1.1
Content-Type: multipart/form-data; boundary=---------------------------7d82751e
bc0858
User-Agent: MyAgent
Host: 127.0.0.1
Content-Length: 210
Cache-Control: no-cache
Cookie: s_pers=%20s_fid%3D6D463DCA024AF939-1C2604BDB2B68135%7C1457266833122%3B%
0s_vs%3D1%7C1394110233124%3B%20s_nr%3D1394108433125-Repeat%7C1425644433125%3B

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - 7 d 8 2 7 5 1 e 2 b c
  8 5 8
 C o n t e n t - D i s p o s i t i o n :   f o r m - d a t a ;   n a m e = " f
 l e " ;   f i l e n a m e = " C : \ t

I just cant understand why is that happening. I've changed this string

sent= HttpSendRequest(hRequest, hdrs, wcslen(hdrs), frmdata, wcslen(frmdata)*sizeof(TCHAR) );

and catched packet with ncat once again. It looked like this:

connect to [127.0.0.1] from validation.sls.microsoft.com [127.0.0.1] 52570
POST /upload/upload.php HTTP/1.1
Content-Type: multipart/form-data; boundary=---------------------------7d82751e2
bc0858
User-Agent: MyAgent
Host: 127.0.0.1
Content-Length: 420
Cache-Control: no-cache
Cookie: s_pers=%20s_fid%3D6D463DCA024AF939-1C2604BDB2B68135%7C1457266833122%3B%2
0s_vs%3D1%7C1394110233124%3B%20s_nr%3D1394108433125-Repeat%7C1425644433125%3B

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - 7 d 8 2 7 5 1 e 2 b c
  8 5 8
 C o n t e n t - D i s p o s i t i o n :   f o r m - d a t a ;   n a m e = " f i
 l e " ;   f i l e n a m e = " C : \ t e s t . t x t "
 C o n t e n t - T y p e :   t e x t / p l a i n

 A A A A A A A A A A A A A A A A A
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 7 d 8 2 7 5 1 e 2 b c
 0 8 5 8 - -

But still something wrong, cause i cant actaully upload data at upload.php script( it is 100% working script, ive checked it with random files). Also that data in netcat looks wierd, why there are spaces between chars? Maybe its because of TCHAR data? So im still confused. How to make it work? I think that problem is in TCHAR type, but there must be the way to make it work with any type. Please help. Also sorry for my english, it is not my native language. All help will be much appreicated!

Was it helpful?

Solution

For HttpSendRequest call, the LPVOID lpOptional should not be in unicode.

Try this:

char pszData[] = "-----------------------------7d82751e2bc0858\r\nContent-Disposition: form-data; name=\"file\"; filename=\"C:\\test.txt\"\r\nContent-Type: text/plain\r\n\r\nAAAAAAAAAAAAAAAAA\r\n-----------------------------7d82751e2bc0858--\r\n";
BOOL sent= HttpSendRequest(hRequest, hdrs, wcslen(hdrs), pszData, strlen(pszData));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top