Link to home
Start Free TrialLog in
Avatar of JoeD77
JoeD77

asked on

Append Data to Remote File Using Wininet

Hello, having some difficulties appending the contents of one file to the contents of another remote file.

code:
// httpPutFile.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include "wininet.h"
#include <stdio.h>
#include<iostream.h>
#pragma comment(lib, "wininet.lib")
int UseHttpSendReqEx(HINTERNET hConnect, char *upFile)
   {
     INTERNET_BUFFERS BufferIn = {0};
     DWORD dwBytesRead;
     DWORD dwBytesWritten;
     BYTE pBuffer[1024]; // Read from file in 1K chunks
     BOOL bRead, bRet;

     BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS );

     HINTERNET hRequest = HttpOpenRequest(hConnect, "PUT", "/logs.bin", NULL, "localhost", NULL, INTERNET_FLAG_HYPERLINK, 0);
     if (!hRequest)
     {
       printf("Failed to open request handle: %lu\n", GetLastError ());
       return FALSE;
     }

     HANDLE hFile = CreateFile (upFile, GENERIC_READ, FILE_SHARE_READ,
         NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
     if (hFile == INVALID_HANDLE_VALUE)
     {
       printf("\nFailed to open local file %s.", upFile);
       return FALSE;
     }

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

     if(!HttpSendRequestEx( hRequest, &BufferIn, NULL, HSR_INITIATE, 0))
     {
       printf( "Error on HttpSendRequestEx %lu\n",GetLastError() );
       return FALSE;
     }

     DWORD sum = 0;
     do
     {
       if  (!(bRead = ReadFile (hFile, pBuffer, sizeof(pBuffer),
           &dwBytesRead, NULL)))
       {
         printf ("\nReadFile failed on buffer %lu.",GetLastError());
         break;
       }
       if (!(bRet=InternetWriteFile( hRequest, pBuffer, dwBytesRead,
           &dwBytesWritten)))
       {
         printf ("\nInternetWriteFile failed %lu", GetLastError());
         break;
       }
       sum += dwBytesWritten;
     }
     while (dwBytesRead == sizeof(pBuffer)) ;

     CloseHandle (hFile);
     printf ("Actual written bytes: %d\n", sum);

     if(!HttpEndRequest(hRequest, NULL, 0, 0))
     {
       printf( "Error on HttpEndRequest %lu \n", GetLastError());
       return FALSE;
     }
     return TRUE;
   }

int main(int argc, char* argv[])
{
	HINTERNET inetOpen;
HINTERNET inetConnect;
inetOpen=InternetOpen("HTTPGET", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
inetConnect=InternetConnect(inetOpen, "mysite.com", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
UseHttpSendReqEx(inetConnect, "c:\\new.txt");
	return 0;
}

Open in new window


the output of the application is "File size is 17
Actual written bytes: 17"

I would assume this means it actually was able to write to the remote file, however, the remote file remains unchanged.

Any input;
Thanks.
ASKER CERTIFIED SOLUTION
Avatar of MedievalWarrior
MedievalWarrior
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial