How do I clear System.Net client DNS cache?



I’m using the .NET WebRequest while changing my HOSTS file. I’m observing that System.Net doesn’t honor those changes – how can I make it do so?

I have a number of servers load-balanced behind a single hostname, let’s say ‘’. I want to target several of them individually, so my program will hard-code the machine-specific IP address in my HOSTS file before sending a request to

For the first server and first request, this works fine. Then my program changes the HOSTS file again:

And I create a new HttpWebRequest. When I send this one off, I can observe in NETMON that it goes to the first IP address ( instead of the expected second one.

Using breakpoints and debug traces, I’ve verified that the correct value does get written to the HOSTS file each time. When I attempt to access from a browser or other program, it does honor the HOSTS file and go to the second IP address.

Using NETMON I’ve verified that requests are going directly to the IP address shown; there is no HTTP proxy.

Since everything else is honoring the changed HOSTS file, I strongly suspect that the System.Net infrastructure has cached the DNS host-IP association for However, I can find no reference to this caching, and know of no way to flush it or turn it off.

I would welcome instructions for dealing with the cache, suggestions for what else might be causing these symptoms, or other proposed diagnostic steps that might be useful.


I finally dug up the obscure command from MSDN that fixes this:

ServicePointManager.DnsRefreshTimeout = 0;

As I unwound all the weird things I’d tried previously, I discovered one other setting that I need along with the one above; on the request object, turn off keep-alive:

request.KeepAlive = false;

Answered By – Bruce

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More