Uri uri = new Uri("http://ftp.nhc.noaa.gov/atcf/aid_public/");
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.Timeout = 1;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Yesterday, this code works fine. Today it doesn't. From what I can tell I think the IP address on the server has changed. When I remove the timout and wait, I can see that an incorrect IP address is being used. However, I can browse to the URL so the DNS is correct on my system (even command line tools resolve to the correct IP) I have tried flush DNS although, to be honest, there is no reason that should work, and it didn't resolve the issue.
So, I am left assuming that somehow, the .NET framework is caching an IP address somewhere. Now, I could reset the machine or clear all of the .NET temporary files but that would leave me with a temporary solution. What I need/want is someway to force .NET, via code, to always resolve the DNS and to not use any cached DNS (Assuming this is the error)
Also, if you have any troubleshooting suggesting that are "in-code" let me know. Rebooting, etc, is not an option because this will eventually be production code so it needs to be robust, I can't hold the hands of the application.
This is a two part issue. The root cause is a proxy server but this was masked by the following scenario:
The code base is shared between a Windows Forms application and an ASP.NET application. When I went from Windows Forms to ASP.NET I got the error. However, when I went back to Windows Forms, I still got the error leading me to conclude that the issue was not caused by ASP.NET. If I am to speculate, the persistence of the issue has to do with a secret cache by .NET, I cheated and cleared the System.Net.ServicePointManager internal service point tables but that did not resolve the issue, rebooting did however. So now that I knew the code worked in Windows Forms but not ASP.NET and sometimes not back-and-forth I had more to go on.
It seems that ASP.NET, does not use the PROXY servers configured for the local machine. My particular workstation uses a proxy configuration file and not a direct proxy so I had to research the proxy. When I use HttpWebRequest under windows forms the proxy is automatically used but when I use the HttpWebRequest under ASP.NET the proxy is ignored. With this little tidbit:
Solves the issue. Overall this makes sense that it is a proxy issue, however, I would expect HttpWebRequest to behave in the same manner under both environments?
Last note, my above code will not work with an authenticated proxy, of course, but if anyone has a similar issue, good place to check.
Rant, why oh why, can't I get uniform consistent behavior and why must their be a myriad of "secret" configuration files that alter "consistent behavior" because, to be honest, I am certain the issue is not Windows Forms vs ASP.NET but secret (ok, not really secret) but numerous configuration files all conspiring to, by default, offer different behavior in the different environments.