+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 12
  1. #1
    Member beginner's Avatar
    Join Date
    Oct 2008
    Location
    Turkey, Istanbul
    Posts
    53

    Cool internet post method using wininet.dll

    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

    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>

    my code is this:
    //************************************************
    void main(int argc, char *argv[])
    {
    if (argc != 3)
    {
    cout << "Usage: download <host> <object>" << endl;
    return;
    }

    HINTERNET hSession = InternetOpen("WinInet Doenload Sample",
    INTERNET_OPEN_TYPE_PRECONFIG,
    NULL,
    NULL,
    0);
    HINTERNET hConnection = InternetConnect(hSession,
    argv[1], // Server
    INTERNET_DEFAULT_HTTP_PORT,
    NULL, // Username
    NULL, // Password
    INTERNET_SERVICE_HTTP,
    0, // Synchronous
    NULL); // No Context

    HINTERNET hRequest = HttpOpenRequest(hConnection,
    "GET",
    argv[2],
    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,
    &dwBufLen,
    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;
    }
    }
    }
    //************************************************
    thank you very much
    regards
    for(p2 = (BYTE*)byte_sequence, len = byte_sequence_len; len && !(*p == backColor && *p2 == foreColor);len--,p++,p2++);
    if(wcscmp(wcslwr((theResult = wcsrchr(ffd.cFileName , L'.') + 1), L"iso")) == 0)
    recursiveMode = wcsicmp(wcsrchr(subfolder, L'\"') + 13, L"/s") ? false : true;

  2. #2
    Junior Member
    Join Date
    Apr 2009
    Posts
    2

  3. #3
    Senior Member albinoskunk's Avatar
    Join Date
    Jun 2008
    Posts
    566
    please use code tags :-) it is hard for people to read what you have coded without them.

  4. #4
    Senior Member mjrod5's Avatar
    Join Date
    Aug 2008
    Location
    In your stack O___O
    Posts
    3,120
    Code:
    my code is this:
    //************************************************
    void main(int argc, char *argv[])
    {
    if (argc != 3)
    {
    cout << "Usage: download <host> <object>" << endl;
    return;
    }
    
    HINTERNET hSession = InternetOpen("WinInet Doenload Sample",
    INTERNET_OPEN_TYPE_PRECONFIG,
    NULL,
    NULL,
    0);
    HINTERNET hConnection = InternetConnect(hSession,
    argv[1], // Server
    INTERNET_DEFAULT_HTTP_PORT,
    NULL, // Username
    NULL, // Password
    INTERNET_SERVICE_HTTP,
    0, // Synchronous
    NULL); // No Context
    
    HINTERNET hRequest = HttpOpenRequest(hConnection,
    "GET",
    argv[2],
    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,
    &dwBufLen,
    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;
    }
    }
    }
    //************************************************
    thank you very much
    regards
    Happy? :p
    In The Oven (err in Development) : Pie
    Quote Originally Posted by uraskiddie View Post
    What are you? Like 10 years old?
    That's complete rubbish, you probably got your depictions of being a "hacker" from a prepubescent forum infested with homosexually-oriented pedophiles.
    Quote Originally Posted by Envy View Post
    Russian?
    Trusted.
    Botnets are like our right hands.
    http://cognitivity.org/

  5. #5
    Member beginner's Avatar
    Join Date
    Oct 2008
    Location
    Turkey, Istanbul
    Posts
    53
    0exception:
    thank you for your links but as you see my code works for GET and I have some problem for POST using the given php code. your links was good but didn't help me. whatever! thank you

    albinoskunk:
    sorry but as you see me name i'm beginner
    I'll use it after this...

    mjrod5:
    thank you for putting my code in code tag dude.

    well, you know, I have a POST method code too that works but it works for php pages that give url parameters. I'm now in university and can not post you the code but if I return to home, I'll post you the code. maybe you can change the can in my way

    thank you all
    regards
    for(p2 = (BYTE*)byte_sequence, len = byte_sequence_len; len && !(*p == backColor && *p2 == foreColor);len--,p++,p2++);
    if(wcscmp(wcslwr((theResult = wcsrchr(ffd.cFileName , L'.') + 1), L"iso")) == 0)
    recursiveMode = wcsicmp(wcsrchr(subfolder, L'\"') + 13, L"/s") ? false : true;

  6. #6
    Member beginner's Avatar
    Join Date
    Oct 2008
    Location
    Turkey, Istanbul
    Posts
    53
    Code:
    #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;
    }
    for(p2 = (BYTE*)byte_sequence, len = byte_sequence_len; len && !(*p == backColor && *p2 == foreColor);len--,p++,p2++);
    if(wcscmp(wcslwr((theResult = wcsrchr(ffd.cFileName , L'.') + 1), L"iso")) == 0)
    recursiveMode = wcsicmp(wcsrchr(subfolder, L'\"') + 13, L"/s") ? false : true;

  7. #7
    Member beginner's Avatar
    Join Date
    Oct 2008
    Location
    Turkey, Istanbul
    Posts
    53
    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
    for(p2 = (BYTE*)byte_sequence, len = byte_sequence_len; len && !(*p == backColor && *p2 == foreColor);len--,p++,p2++);
    if(wcscmp(wcslwr((theResult = wcsrchr(ffd.cFileName , L'.') + 1), L"iso")) == 0)
    recursiveMode = wcsicmp(wcsrchr(subfolder, L'\"') + 13, L"/s") ? false : true;

  8. #8
    Senior Member albinoskunk's Avatar
    Join Date
    Jun 2008
    Posts
    566
    instead of using all those strcat's like this:

    Code:
     
    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");
    try replace them with a single sprintf, should tighten up your code abit :-)

    Code:
           
    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";
    
    sprintf(hdrs, "%s\"\r\nContent-Type: text/plain\r\n\r\n %s \r\n-----------------------------23281168279961--\r\n", fname, content);
    i typed the above on my laptop in the reply box so i expect there is a typo in there ;-)

    i have yet to really read your code fully, but those strcat's just seemed to take up quite some space so i thought i would show you a small "fix".

    good luck with solving your problem, stick at it and i am sure you will over come the problem.

    -AlbinoSkunk.

  9. #9
    Junior Member
    Join Date
    Apr 2009
    Posts
    2
    Fixed and working...

    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <wininet.h>
    
     
    int main(void) {
    	char url[] = "upload.php?name=value";
    	char contenttype[] = "content-type: multipart/form-data; " 
    	"boundary=-----------------------------23281168279961\r\n";
    	char body[1024] = "-------------------------------23281168279961\r\n"
    	"content-disposition: form-data; name=\"file\"; filename=\"test.txt\"\r\n"
    	"content-type: text/plain\r\n"
    	"\r\n"
    	"these line is in test.txt that should be uploaded. this line is my file's content"
    	"\r\n"
    	"-------------------------------23281168279961"
    	"\r\n";
    
    	HINTERNET hSession = InternetOpen("",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
        
    	if (!hSession) { 
    		fprintf(stderr, "InternetOpen failed.\r\n");
    		return -1;
    	}
    
    	HINTERNET hConnect = InternetConnect(hSession, "localhost",INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
        
    	if (!hConnect) {
    		fprintf(stderr, "InternetConnect failed.\r\n");
    		return -1;
    	}
    	
    	HINTERNET hRequest = HttpOpenRequest(hConnect, "POST", url, "HTTP/1.0", NULL, (const char**)"*/*", 0, 0);
        
    	if (!hRequest) {
    		fprintf(stderr, "InternetRequest failed.\r\n");
    		return -1;
    	}
    	
    	if (!HttpAddRequestHeaders(hRequest, contenttype, strlen(contenttype), 0)) {
    		fprintf(stderr, "HttpAddRequestHeaders failed.\r\n");
    		fprintf(stderr, "Error: %i", GetLastError());
    		return -1;
    	}
    
    	
    	if (!HttpSendRequest(hRequest, NULL, 0, body, strlen(body))) {
    		fprintf(stderr, "HttpSendRequest failed.\r\n");
    		fprintf(stderr, "Error: %i", GetLastError());
    	}
        
        InternetCloseHandle(hSession);
        InternetCloseHandle(hConnect);
        InternetCloseHandle(hRequest);
    	
        return 0;
    }

  10. #10
    Member beginner's Avatar
    Join Date
    Oct 2008
    Location
    Turkey, Istanbul
    Posts
    53
    thank you very much 0exception, for answering me

    my php code is this:
    Code:
    <?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>
    I tested your code, didn't work! and I changed a lot your code and didn't work again!
    I think if you test for my php would not work. this did not work on my local apache (

    so, any idea my friend?
    thank you and other people
    yours sincerely
    for(p2 = (BYTE*)byte_sequence, len = byte_sequence_len; len && !(*p == backColor && *p2 == foreColor);len--,p++,p2++);
    if(wcscmp(wcslwr((theResult = wcsrchr(ffd.cFileName , L'.') + 1), L"iso")) == 0)
    recursiveMode = wcsicmp(wcsrchr(subfolder, L'\"') + 13, L"/s") ? false : true;

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. idHTTP.post Problem
    By B!0S in forum Delphi Help
    Replies: 3
    Last Post: 29-07-2009, 12:44
  2. Replies: 9
    Last Post: 03-05-2008, 03:55
  3. how can i post my picture?
    By gemini in forum OpenSC-News
    Replies: 2
    Last Post: 13-03-2006, 02:51
  4. Small WinInet, Windows, SysUtils.
    By n0v4 in forum General Programming Help
    Replies: 7
    Last Post: 19-01-2006, 17:27
  5. Post your new forums here ( must be cool )
    By TheGrunt in forum Off-Topic
    Replies: 24
    Last Post: 13-09-2005, 15:18

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 ©2011, Crawlability, Inc.