In a .NET application I am writing, I need to detect whether a particular URL is available. For the average user with a default DNS server, an invalid address would end up throwing a WebException
. However, at home I am using OpenDNS. When I request an invalid address at home, the automatic OpenDNS redirect makes .NET believe the request succeeded. How can I detect when the DNS server is not giving me the address I requested?
Here's some of the code:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://...");
request.AllowAutoRedirect = false;
try
{
WebResponse response = request.GetResponse();
using (Stream stream = response.GetResponseStream())
{
// Do work
...
}
}
catch (WebException ex)
{
// Handle normal errors
...
}