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
...
}
Have you tried looking at the HTTP status code that is returned?
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
using (Stream stream = response.GetResponseStream())
{
// Do work
...
}
}
else
{
// Error
}
Check to see if the WebResponse.ResponseUri value matches the original URL requested. http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.responseuri.aspx
If the ResponseUri matches the original URL, then the best you can do is look at the response text and see if it "looks" like the OpenDNS page.
加载中,请稍侯......
精彩评论