In general, the problem is this: I'm trying to get data from a text file on the server (~ 10 bytes) using WinInet in such a way that:

  • There is a function that returns the contents of the file.
  • A new thread is created with this function in a looped form, with a break of 10 seconds.

The request is sent, the answer comes, but if the contents of the file has changed - the answer for some reason does not change.

Here is the code itself:

#include <iostream> #include <windows.h> #include <wininet.h> #include <thread> #pragma comment(lib,"wininet") using namespace std; string flag; string getresponse(){ //сама функция получения ответа string ret; bool ok = false; HINTERNET hInternet = ::InternetOpen( TEXT("WinInet Test"), INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL, 0); if (hInternet != NULL) { HINTERNET hConnect = ::InternetConnect( hInternet, TEXT("www.applang.tk"), INTERNET_DEFAULT_HTTP_PORT, NULL,NULL, INTERNET_SERVICE_HTTP, 0, 1u); if (hConnect != NULL) { HINTERNET hRequest = ::HttpOpenRequest( hConnect, TEXT("GET"), TEXT("baget/com"), NULL, NULL, 0, INTERNET_FLAG_KEEP_CONNECTION, 1); if (hRequest != NULL) { BOOL bSend = ::HttpSendRequest(hRequest, NULL,0, NULL,0); if (bSend) { for (;;) { char szData[1024]; DWORD dwBytesRead; BOOL bRead = ::InternetReadFile( hRequest, szData,sizeof(szData)-1, &dwBytesRead); if (bRead == FALSE || dwBytesRead == 0) break; szData[dwBytesRead] = 0; ret = string(szData,strlen(szData)); ok = true; } } ::InternetCloseHandle(hRequest); } ::InternetCloseHandle(hConnect); } ::InternetCloseHandle(hInternet); } return ret; } void syncserv(string arg){ //вспомогательная функция while(true){ cout<<"SENT REQUEST"<<endl; flag = getresponse(); cout <<"GOT RESPONSE: "<<flag<<endl; Sleep(10000); } } main(){ thread t1(syncserv, "something"); //создаем поток getch(); } 
  • Code copied and not even read about the functions used in it WinApi? I recommend to read, help. Especially ask about the various flags used in functions. Also google such a thing as CACHE. And yet ... Well, this question has absolutely nothing to do with C ++. Change the tag from C ++ to Winapi. - Max ZS
  • Yes, about caching, I thought I would look. - GuyInSombrero

1 answer 1

I do not know how you searched for information about the cache, but for each of these functions in MSDN there is information about caching.

For HttpSendRequest, there are INTERNET_FLAG_FROM_CACHE and INTERNET_FLAG_OFFLINE flags.

For InternetConnect, caching is mentioned in a note.

For the HttpOpenRequest function there is INTERNET_FLAG_CACHE_IF_NET_FAIL, INTERNET_FLAG_NO_CACHE_WRITE, INTERNET_FLAG_PRAGMA_NOCACHE, INTERNET_FLAG_RELOAD.

And for the functions HttpSendRequest , the InternetReadFile cache is mentioned in the notes.

No need to mention what is supposedly written there when it is impossible to connect to the network. Yes, in some cases, the behavior is described in offline / offline mode and in the event of a network error and unavailability. Then, too, the data is taken from the cache. But we do not know all the conditions of your testing.

And the most interesting thing is if you read the HttpOpenRequest and InternetReadFile notes.

 Note that caching happens automatically unless the original request to open the data stream set the INTERNET_FLAG_NO_CACHE_WRITE flag. If a verb other than "GET" or "POST" is specified, HttpOpenRequest automatically sets INTERNET_FLAG_NO_CACHE_WRITE and INTERNET_FLAG_RELOAD for the request. 

One could write all this in the comments, but, unfortunately, they do not contain so much text.