views:

105

answers:

2

Is there any to make sure that DNS errors has been occurred after getting WebException in the following code?

WebRequest request = WebRequest.Create(uri);
....
WebResponse response = request.EndGetResponse(asyncResult);

String comparison might be one way. By checking the error message we can be sure. But depending on culture the message string can vary. So this may not be the best way for checking DNS error.

+2  A: 

Instead of relying (ever!) on the response message received, I would rely on the StatusCode of the WebResponse received. An HTTP status code in the range 4xx (400-499) would be indicative of DNS issues or errors in locating the resource.

Cerebrus
+2  A: 

One of the the WebExceptionStatus values is NameResolutionFailure. This indicates DNS errors.

Martin v. Löwis
Sweet! Didn't know that. +1
Cerebrus