当 HttpWebResponse 无法连接到 server/internet 时如何在 C# 中显示消息框

How to show messagebox in c# when HttpWebResponse unable to connect to the server/internet

我是 C# 的新手,正在开发 window 从 Web 抓取 URL 的应用程序。应用程序需要 Internet 连接才能从 Internet 收集 URL。问题是当没有互联网 connection.And 应用程序时会出现此类错误。

An unhandled exception of type 'System.Net.WebException' occurred in System.dll Additional information: The remote name could not be resolved: '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 将捕获所有这些。你需要找到每一个可能的异常并相应地处理它。

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

  try
   {
         //your code here
    }
   catch (WebException ex)
   {
         MessageBox.Show("No internet available");
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error has occured");
    }