Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear WinInet DNS cache programmatically

WinINet library caches IP address for any URL accessed over it. Because of this, when IP address for that URL gets changed then also WinInet library's HttpSendRequest goes to older IP address.

And, if older IP is responding, then WinINet will send all http request to older IP only.

Is there any way to force clean DNS cache of WinInet?

Or

Is there any way to force WinINet to send HTTP request to specified IP address (as we are able to get newer IP using gethostbyname())?

Note :

  1. gethostbyname is giving me newer IP address, So this behaviour is happening of WinINet's caching.
  2. I have tried "method 2" suggested in this MS article, but it didn't help
  3. Sample code
like image 669
Pradeep Avatar asked Jun 28 '26 13:06

Pradeep


1 Answers

You can try to use the flag when connecting:

INTERNET_FLAG_DONT_CACHE = 0x04000000 Does not add the returned entity to the cache. This is identical to the preferred value, INTERNET_FLAG_NO_CACHE_WRITE.

Or you can take a look at the DeleteUrlCacheEntry from the WinInet documentation here

I beleave this should do the trick.

--UPDATE

From this doc I've seen that there is an better flag to use it, look at the:

INTERNET_FLAG_PRAGMA_NOCACHE Forces the request to be resolved by the origin server, even if a cached copy exists on the proxy.

--UPDATE

As tested by @Pradeep you can change this registry keys to work it: HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\DnsCacheEnabled ServerInfoTimeOut and DnsCacheTimeout to 0.

like image 53
Diego Garcia Avatar answered Jul 01 '26 12:07

Diego Garcia