none
send text file using InternetWriteFile

    Question

  • Hi,

    I would like to send a text file from my local directory to the server. How could I do that?

    This is what I have now, and it is not working:

        INTERNET_BUFFERS BufferIn = {0};
        BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS );

        HINTERNET   hSession, hConnection, hRequest;
        wchar_t     hdr_buf[1024], error_buf[128];
        wchar_t     query_buf[1024] = {0,};
        BOOL        result;

        DWORD dwBytesRead;
        DWORD dwBytesWritten;
        BYTE pBuffer[1024];

        hSession = InternetOpen( "name", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
        if( hSession == NULL )
        {
            MessageBox( g_hWndMain, L"HTTP Open failed!", L"HTTP", MB_OK );
            return FALSE;
        }

        hConnection = InternetConnect( hSession, "server name",
                                    INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
                                    INTERNET_SERVICE_HTTP, 0, 0 );
        if( hConnection == NULL )
        {
            wsprintf( error_buf, L"HTTP Connection failed (%d)", GetLastError() );
            MessageBox( g_hWndMain, error_buf, L"HTTP", MB_OK );
            InternetCloseHandle( hSession );
            return FALSE;
        }

        hRequest = HttpOpenRequest( hConnection, L"POST", "object name?",
                                    HTTP_VERSION, NULL, 0, INTERNET_FLAG_KEEP_CONNECTION &&
                                    INTERNET_FLAG_NO_CACHE_WRITE, 0 );
        if( hRequest == NULL )
        {
            wsprintf( error_buf, L"HTTP OpenRequest(POST) failed (%d)", GetLastError() );
            MessageBox( g_hWndMain, error_buf, L"HTTP", MB_OK );
            InternetCloseHandle( hConnection );
            InternetCloseHandle( hSession );
            return FALSE;
        }


        HANDLE hFile = CreateFile(TEXT("\\mytext.txt"), //File name
                                    GENERIC_READ, //Desired Access
                                    FILE_SHARE_READ, //Share Mode
                                    NULL, //Security Attribute
                                    OPEN_EXISTING, //Creation Disposition
                                    FILE_ATTRIBUTE_NORMAL, //Flags and Attributes
                                    NULL); //Template File

        if(hFile == INVALID_HANDLE_VALUE)
        {
            wsprintf( error_buf, L"file open failed (%d)", GetLastError() );
            MessageBox( g_hWndMain, error_buf, L"HTTP", MB_OK );
            InternetCloseHandle( hConnection );
            InternetCloseHandle( hSession );
            return FALSE;
        }

        BufferIn.dwBufferTotal = GetFileSize(hFile, NULL);
        printf("File size is %d\n", BufferIn.dwBufferTotal);


        if(HttpSendRequestEx(hRequest, NULL, 0, NULL,0))
        {
            DWORD sum = 0;
            do
            {
                if(!(ReadFile(hFile, pBuffer, sizeof(pBuffer), &dwBytesRead, NULL)))
                {
                    MessageBox( g_hWndMain, L"Error Read File", L"HTTP", MB_OK );
                    break;
                }
                if(!(InternetWriteFile(hRequest, pBuffer, dwBytesRead, &dwBytesWritten)))
                {
                    MessageBox( g_hWndMain, L"Error Write File", L"HTTP", MB_OK );
                    break;
                }

                sum += dwBytesWritten;
            } while(dwBytesRead == sizeof(pBuffer));

        }

        InternetCloseHandle( hRequest );
        InternetCloseHandle( hConnection );
        InternetCloseHandle( hSession );


    I'm not sure what is wrong. One thing I'm really unsure about is the third parameter in HttpOpenRequest(). What should the "object name" be? Since I want to send a text file over to the server, could this object name be the name of the text file I would like to create on the server, or does it have to be a php script?

    I also would like to ask, is InternetWriteFile the correct way to send file over to the server? I read somewhere that I don't need InternetWriteFile, just need HttpSendRequest. Is that true? Right now InternetWriteFile is giving me errors, and I don't know the reason of it. Additionally, I don't see any new text files appear on the server.

    Thanks.
    Sheri
    Saturday, August 22, 2009 12:32 AM

Answers

All replies

  • The "object name" parameter to HttpOpenRequest() should b ethe file name you want to send. From http://msdn.microsoft.com/en-us/library/aa384233%28VS.85%29.aspx "lpszObjectName [in] A pointer to a null-terminated string that contains the name of the target object of the specified HTTP verb. This is generally a file name, an executable module, or a search specifier."

    What error is InternetWriteFile() giving you? Use GetLastError() after a failed call to InternetWriteFile() to find the error code. The error code will probably match one on this page: http://msdn.microsoft.com/en-us/library/aa385465%28VS.85%29.aspx Use that to diagnose the defect.

    -PaulH

    Monday, August 24, 2009 2:35 PM
  • Hi,

    I just checked on this, actually InternetWriteFile() is not giving error. The problem is, the php script that I write (which was the "object name" file) will exit after the call to HttpSendRequestEx(), therefore it will no longer be open for the InternetWriteFile() function. How should I solve this problem?

    -Sheri
    Friday, September 04, 2009 8:31 AM