Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dns Settings changed while program running

I have a program which uses WebRequest for accessing http sites. If I start the program perform some webRequests and then change the Dns settings of my machine, the program will not change dns-servers

Like

 WebRequest.Create("http://www.google.com");

....Change Dns settings for my network interface to something invalid or capturing portal....

 WebRequest.Create("http://www.google.com"); 
 // Still uses original dns server for dns lookup (or cache)

ipconfig flushdns makes no difference

Any way I can force the WebRequest to use the actual dns server for dns lookups?

EDIT: It seems that a restart of the windows service for DnsClient cache does the trick. Quite hardcore though

like image 766
svrist Avatar asked Nov 05 '22 15:11

svrist


1 Answers

Internally, every server is abstracted by a ServicePoint class. So, once you have created a ServicePoint, either explicitly, or implicitly, it does not change.

Also, it might be caching the previous connection and using it for the subsequent request.

You can try setting

HttpWebRequest.KeepAlive = false

and

HttpWebRequest.ConnectionGroupName = String.Format("connection-{0}", ++index);

and see if that forces .NET to create a new connection every time.

If that doesnt work, try implementing a BindIPEndPointDelegate() method and attaching it to the webrequest. Then, for each request, .NET will call that delegate to resolve the endpoint IPAddress, and you can do a DNS.Resolve() in that delegate.

like image 118
feroze Avatar answered Nov 15 '22 04:11

feroze