Click to See Complete Forum and Search --> : send text file using InternetWriteFile


shericn
August 21st, 2009, 07:35 PM
Hi codeguru people,

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

dc_2000
August 23rd, 2009, 03:29 AM
OK, Sheri, several things.

(1) Put your code into code blocks (http://www.codeguru.com/forum/misc.php?do=bbcode#code). It is impossible to read it.

(2) If something gets you errors, you look up what they are and if you can't alter your code accordingly, post it here as well.

(3) Why not use sockets? InternetWriteFile() is a "higher-level" wrapper function that may have a bunch of its own quirks. Look on the net, there's a bunch of examples of how to do it with sockets:
http://www.codeproject.com/KB/cpp/WSFileTransfer.aspx

shericn
August 24th, 2009, 03:24 AM
Hi

Sorry about not using code blocks. I have pasted the code below.

I cannot use the things shown in the link, http://www.codeproject.com/KB/cpp/WSFileTransfer.aspx, because I'm programming for Windows Mobile, using windows API. Do you know how I could do this using InternetWriteFile() or HttpSendRequest?



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 );





Thanks.
Sheri