开发者

Re-writing the Hosts file does not update the next IP connection

开发者 https://www.devze.com 2023-02-11 16:29 出处:网络
We have a written a C# application that communicates with any one of a group of IP in the cloud. Any one of which may not be working.We use the URL of the address as the IIS server is expecting a Host

We have a written a C# application that communicates with any one of a group of IP in the cloud. Any one of which may not be working. We use the URL of the address as the IIS server is expecting a Host Header Name in order to route to the correct application interface.

So we set the Hosts file to point the URL at an IP. We then send a command at the URL to get back the server time.

This tells us the connection is working.

If we don't get a response we assume the connection is dead. We then write a new IP from a list into the Hosts file and we try again.

This is where we hit a bug. The application开发者_运维知识库 doesn't seem to see the Hosts file has changed and uses the old (bad) IP.

There is no caching built into the application so we are assuming that Windows is caching for us.

We've tried to flush caches with:

ipconfig /flushdns arp -d * nbtstat -R

We still get the same problem. Any thoughts on how to clear the cache?


If you can't address this at the server end (e.g. a load balancer, etc), then just use the IP address list in your code:

var req = HttpWebRequest.Create("http://" + IPAdd.ToString() + "/path_to_query_time");
((HttpWebRequest)req).Host = "yourhostheaderhere";
var resp = req.GetResponse();
//If things have gone wrong here, change IPAdd to the next IP address and start over.

Don't go messing with the users settings to try and solve a problem in your application that's of your own making.

0

精彩评论

暂无评论...
验证码 换一张
取 消