[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


Append Data to Remote File Using Wininet

Posted on 2011-03-19
Medium Priority
Last Modified: 2012-05-11
Hello, having some difficulties appending the contents of one file to the contents of another remote file.

// httpPutFile.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <windows.h>
#include "wininet.h"
#include <stdio.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,
     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;
       if  (!(bRead = ReadFile (hFile, pBuffer, sizeof(pBuffer),
           &dwBytesRead, NULL)))
         printf ("\nReadFile failed on buffer %lu.",GetLastError());
       if (!(bRet=InternetWriteFile( hRequest, pBuffer, dwBytesRead,
         printf ("\nInternetWriteFile failed %lu", GetLastError());
       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;
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;
Question by:JoeD77
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
1 Comment

Accepted Solution

MedievalWarrior earned 2000 total points
ID: 35179674
It's documented that you should resolve the full address as in: www.mysite.com and I don't see any calls to InternetCloseHandle() which is used to end the data transfer.

Open in new window


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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

649 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