当HttpWebResponse无法连接到服务器/互联网时,如何在c#中显示messagebox

当HttpWebResponse无法连接到服务器/互联网时,如何在c#中显示messagebox,c#,server,webrequest,httpwebresponse,C#,Server,Webrequest,Httpwebresponse,我是C#新手,正在开发从Web上抓取URL的窗口应用程序。应用程序需要Internet连接才能从Internet收集URL。问题是在没有internet连接的情况下发生。应用程序显示这种类型的错误 中发生类型为“System.Net.WebException”的未处理异常 System.dll其他信息:无法找到远程名称 解析:“www.google.com” 问题是我写了什么代码告诉用户,没有互联网连接。而不是显示这种类型的错误。 这是我正在编写的代码 listBox1.Items.Clear(

我是C#新手,正在开发从Web上抓取URL的窗口应用程序。应用程序需要Internet连接才能从Internet收集URL。问题是在没有internet连接的情况下发生。应用程序显示这种类型的错误

中发生类型为“System.Net.WebException”的未处理异常 System.dll其他信息:无法找到远程名称 解析:“www.google.com”

问题是我写了什么代码告诉用户,没有互联网连接。而不是显示这种类型的错误。 这是我正在编写的代码

listBox1.Items.Clear();
            StringBuilder sb = new StringBuilder();
            byte[] ResultsBuffer = new byte[8192];
            string SearchResults = "http://www.google.com/search?num=1000&q=" + txtKeyWords.Text.Trim();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(SearchResults);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream resStream = response.GetResponseStream();
            string tempString = null;
            int count = 0;
            do
            {
                count = resStream.Read(ResultsBuffer, 0, ResultsBuffer.Length);
                if (count != 0)
                {
                    tempString = Encoding.ASCII.GetString(ResultsBuffer, 0, count);
                    sb.Append(tempString);
                }
            }
            while (count > 0);
            string sbb = sb.ToString();

            HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
            html.OptionOutputAsXml = true;
            html.LoadHtml(sbb);
            HtmlNode doc = html.DocumentNode;

            foreach (HtmlNode link in doc.SelectNodes("//a[@href]"))
            {
                //HtmlAttribute att = link.Attributes["href"];
                string hrefValue = link.GetAttributeValue("href", string.Empty);
                if (!hrefValue.ToString().ToUpper().Contains("GOOGLE") && hrefValue.ToString().Contains("/url?q=") && hrefValue.ToString().ToUpper().Contains("HTTP://"))
                {
                    int index = hrefValue.IndexOf("&");
                    if (index > 0)
                    {
                        hrefValue = hrefValue.Substring(0, index);
                        listBox1.Items.Add(hrefValue.Replace("/url?q=", ""));
                    }
                }
            }

您可以这样做:

//Include everything that could possibly throw an exception in the try brackets
try
{
   HttpWebRequest request = (HttpWebRequest)WebRequest.Create(SearchResults);
   HttpWebResponse response = (HttpWebResponse)request.GetResponse();
   Stream resStream = response.GetResponseStream();
}
catch (Exception e) // Here we are catching your bug-the unhandled exception
{
  MessageBox.Show("You do not have an internet connection");
}

这就是为什么我的答案不完整,需要你做更多的工作:除了没有互联网,还有更多的例外。这次试捕将捕获所有的鱼。您需要找到每一个可能的异常并进行相应的处理。

首先尝试根据您的错误捕获特殊异常,然后对可能发生的任何其他错误进行常规捕获,然后


尝试查看异常处理一个Try-catch将捕获“bug”,并给您显示一个消息框的空间,该消息框说“不”internet@SethKitchen你能参考这里的代码吗???@Shah你在使用wpf、通用或控制台应用程序还是什么?谢谢你的回复。你让我开心。它确实有效。
  try
   {
         //your code here
    }
   catch (WebException ex)
   {
         MessageBox.Show("No internet available");
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error has occured");
    }