This is my code:
string Url = "http://illution.dk/";
WebClient Http = new WebClient();
Http.DownloadStringAsync(new Uri(Url));
Http.DownloadStringCompleted += new DownloadStringCompletedEventHandler(GetModelTypeResponse);
If I run this on a machine with no internet connection an error will be thrown. "Hostname could not be resolved"
Is there any way I can remove this error message? Or check if there isn't an internet connection?
Edit 1
try
{
ComputerInfo ComputerInfoComp = new ComputerInfo();
string Url = "http://illution.dk/"
WebClient Http = new WebClient();
Http.DownloadStringAsync(new Uri(Url));
Http.DownloadStringCompleted += new DownloadStringCompletedEventHandler(GetModelTypeResponse);
ComputerInfoComp = null;
}
catch (Sys开发者_如何学Gotem.Net.WebException e)
{
//
}
Check e.Error
in your GetModelTypeResponse
handler method. If e.Error
is of type WebException
then check webException.Status
value. The value will be WebExceptionStatus.NameResolutionFailure
in case of "Hostname could not be resolved" exception
public void GetModelTypeResponse(object sender, DownloadStringCompletedEventArgs e)
{
var webException = e.Error as WebException;
if (webException != null &&
webException.Status == WebExceptionStatus.NameResolutionFailure)
{
// log
return; // ignore
}
// proceed
..
}
Yes, by catching that particular exception. See http://msdn.microsoft.com/en-us/library/ms173160(v=vs.80).aspx
Put your code in a try...catch block. Google exceptions
for more information.
精彩评论