Irisciences
Irisciences

Reputation: 308

C++ WinINet InternetReadFile function refresh

I am trying to get the content of a file using WinHTTP in C++. The file is a XML File and is generated by a executable on a server.

The code for init, connect and even read a file on the specified server address is working.

// Connect to internet.
m_hInternet = InternetOpen(L"HTTPRIP",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);

// Check if worked.
if( !m_hInternet ) 
    return;

// Connect to selected URL.
m_hUrl = InternetOpenUrlA(m_hInternet, strUrl.c_str(), NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RESYNCHRONIZE, 0);

// Check if worked.
if( !m_hUrl ) 
    return;

if( InternetReadFile(m_hUrl, buf, BUFFER_SIZE, &bytesread) && bytesread != 0 )
{
    // Put into std::string.
    strData = std::string(buf,buf+bytesread);
}

Now I want to update the file (same address). The server update the file at 50Hz and I want my code to be able to ReadFile only if it has been updated by the server. Can InternetReadFile do that kind of thing? Maybe with a FLAG but I didn't find a thing on MSDN.

Thanks for your help.

Upvotes: 1

Views: 486

Answers (2)

Matt
Matt

Reputation: 15091

  1. If HTTP server has a page with info (e.g. timestamp) on this file (no matters that a file is generated; the page may be generated too), you may examine this page.
  2. As you know that server updates the file with (nearly) constant speed, your app may just use the timer.

P.S. I doubt if there's really a sense in reading some file 50 times every second.

Upvotes: 1

Prof. Falken
Prof. Falken

Reputation: 24887

There is no way in the HTTP protocol for you directly do that, hence there is no such function in WinHTTP. The easiest solution might be to download the file and see if it's changed, if the file is relatively small, or if the file is large, let the server which writes the file, also write a timestamp, checksum or counter increment file next to it.

Then your code would download the checksum file, see if it's changed, and in that case download the original file.

Or another solution would be to put a timestamp or similar data in the beginning of the XML file, and stop downloading the file if the timestamp (or checksum) is not updated. (This comes with its own drawbacks of course, you may have to write your own parser.)

Upvotes: 1

Related Questions