Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Internet connection post method using wininet.dll

Posted on 2009-04-05
4
Medium Priority
?
2,398 Views
Last Modified: 2013-12-03
Hello
I have a application that has a file submit section and I want to post a file to my website. I have a php page that accepts incoming file and stores it in the server. I want to do it using wininet.dll not C++ library functions in winsock2.h and ws2_32.dll
so, I got the first code of jkr in here:
http://www.experts-exchange.com/Programming/Languages/CPP/Q_21608494.html
it works for GET method but I want to post a file using it. so, I changed a little it but dunno how to post a file using it yet! :(

the php page's code is:
<?php
$msg = copy($_FILES[MyFile][tmp_name],$_FILES[MyFile][name]) ? "Yes" : "No";
echo $msg;
?>
<form ENCTYPE="multipart/form-data" ACTION="" METHOD="POST">
<input NAME="MyFile" TYPE="file">
<input VALUE="Upload!" TYPE="submit"></form>


thank you very much
regards
HINTERNET hSession = InternetOpen("Unknown",INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
    HINTERNET hConnection = InternetConnect(hSession,
                                            "localhost", //argv[1],  // Server
                                            INTERNET_DEFAULT_HTTP_PORT,
                                            NULL,     // Username
                                            NULL,     // Password
                                            INTERNET_SERVICE_HTTP,
                                            0,        // Synchronous
                                            NULL);    // No Context
    HINTERNET hRequest = HttpOpenRequest(hConnection,
                                         "GET",
                                         "uploader.php",
                                         NULL,    // Default HTTP Version
                                         NULL,    // No Referer
                                         (const char**)"*/*\0", // Accept anything
                                         0,       // Flags
                                         NULL);   // No Context
    HttpSendRequest(hRequest,
                    NULL,    // No extra headers
                    0,       // Header length
                    NULL,    // No Body
                    0);      // Body length
    DWORD dwContentLen;
    DWORD dwBufLen = sizeof(dwContentLen);
    if(HttpQueryInfo(hRequest,
                      HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,
                      (LPVOID)&dwContentLen, //Address of the buffer that receives the information
                      &dwBufLen, //Address of a value that contains the length of the data buffer
                      0))
    {
        // You have a content length so you can calculate percent complete
        char *pData = (char*)GlobalAlloc(GMEM_FIXED, dwContentLen + 1);
        DWORD dwReadSize = dwContentLen;
        DWORD dwBytesRead;
 
        InternetReadFile(hRequest, pData, dwReadSize, &dwBytesRead);
        // Null terminate data
        pData[dwContentLen] = 0;
 
        // Display
        cout << endl << "Download Complete" << endl;
        cout << pData;
    }
    else
    {
        DWORD err = GetLastError();
        // No content length...
        // Just read until we are done.
        char pData[100];
        DWORD dwBytesRead = 1;
        while (dwBytesRead)
        {
            InternetReadFile(hRequest, pData, 99, &dwBytesRead);
            pData[dwBytesRead] = 0;
            cout << pData;
        }
    }

Open in new window

0
Comment
Question by:CSecurity
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 24072662
Hm, the above code would *download* a file from the server, that's not really what you want. After base64-encoding the file you want to send, you can use the code at http://support.microsoft.com/kb/165298 ("How to simulate a Form POST request by using WinInet") to send it like
   // for clarity, error-checking has been removed
   HINTERNET hSession = InternetOpen("MyAgent",
      INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
   HINTERNET hConnect = InternetConnect(hSession, _T("ServerNameHere"),
      INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
   HINTERNET hRequest = HttpOpenRequest(hConnect, "POST",
      _T("FormActionHere"), NULL, NULL, accept, 0, 1);
   HttpSendRequest(hRequest, hdrs, strlen(hdrs), frmdata, strlen(frmdata));
   // close any valid internet-handles

Open in new window

0
 
LVL 17

Accepted Solution

by:
CSecurity earned 0 total points
ID: 24075052
thank you jkr
I couldn't use your code for my php :(
I changed it but didn't work. I have a project in VB that I can successfully can upload a file using this header:
-----------------------------23281168279961
Content-Disposition: form-data; name="MyFile"; filename="test.txt"
Content-Type: text/plain

these lines are in test.txt that should be uploaded
-----------------------------23281168279961--


as you see, "these lines are in test.txt that should be uploaded" is text that is in test.txt that my application reads from the file.

so, how can I change it?
thank you very much
regards
#include <windows.h>
#include <wininet.h>
#include <tchar.h>
 
#pragma comment(lib,"wininet.lib")
 
int main()
{
	static TCHAR frmdata[] = _T("name=John+Doe&userid=hithere&other=P%26Q");
    //static LPSTR accept[2]={"*/*", NULL};
 
    // for clarity, error-checking has been removed
 
	static TCHAR hdrs[2048] = _T("-----------------------------23281168279961\r\n");
	strcat(hdrs,"Content-Disposition: form-data; name=\"MyFile\"; filename=\"");
	TCHAR fname[] = "test.txt";
	TCHAR content[] = "this is a test";
	strcat(hdrs,fname);
	strcat(hdrs,"\"\r\n");
	strcat(hdrs,"Content-Type: text/plain\r\n\r\n");
	strcat(hdrs,content);
	strcat(hdrs,"\r\n");
	strcat(hdrs, "-----------------------------23281168279961--\r\n");
	   
	HINTERNET hSession = InternetOpen("MyAgent",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
    HINTERNET hConnect = InternetConnect(hSession, _T("localhost"),INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
    HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST",_T("uploader.php"), NULL, NULL, (const char**)"*/*\0", 0, 1);
    HttpSendRequest(hRequest, hdrs, strlen(hdrs), frmdata, strlen(frmdata));
    // close any valid internet-handles
    InternetCloseHandle(hSession);
    InternetCloseHandle(hConnect);
    InternetCloseHandle(hRequest);
    return 0;
}

Open in new window

0
 
LVL 17

Author Comment

by:CSecurity
ID: 24075811
indeed my problem is that my file content should be between header and i dunno what to do :(

-----------------------------23281168279961
Content-Disposition: form-data; name="MyFile"; filename="test.txt"
Content-Type: text/plain

these line is in test.txt that should be uploaded. this line is my file's content
-----------------------------23281168279961--


regards
0
 
LVL 17

Author Comment

by:CSecurity
ID: 24091287
any idea? :(
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question