0

I am new to C# and working on window application that scrape URLs from the Web. Application needs Internet connection in order to collect URLs from Internet. The problem is when occurs when there is no internet connection.And application shows this type of Error.

An unhandled exception of type 'System.Net.WebException' occurred in System.dll Additional information: The remote name could not be resolved: 'www.google.com'

The question is what piece of code I write that tell to user, there is no internet connection. Rather then showing this type of Bug. Here is the code I am working on.

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=", ""));
                    }
                }
            }
Shah
  • 15
  • 5

2 Answers2

0

You can do something like this:

//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");
}

Here is why my answer is not complete and needs more work from you: There are more exceptions than just no internet that you can receive. This try catch will catch all of them. You need to find every possible exception and handle it accordingly.

Seth Kitchen
  • 1,526
  • 19
  • 53
0

first try to catch special exception according to your error then a general catching for any other errors that might occur, 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");
    }
Community
  • 1
  • 1
Emad Khalil
  • 803
  • 1
  • 8
  • 14