0

I have problem with web client connection. On most of the sites there is no problem to get title of page, but when I testing it found one site that raise error "The underlying connection was closed: An unexpected error occurred on a receive." concrete site is : "https://www.modbee.com/opinion/letters-to-the-editor/article111712867.html".

private string GetTitle(string link)
    {
        try
        {
            string html = webClient.DownloadString(link);
            Regex reg = new Regex("<title>(.*)</title>");
            MatchCollection m = reg.Matches(html);
            if (m.Count > 0)
            {
                return m[0].Value.Replace("<title>", "").Replace("</title>", "");
            }
            else
                return "";
        }
        catch (Exception e)
        {
            return labelError.Text = "You should insert correct URL\n";
        }

    }
cole
  • 75
  • 5
  • You could check the server result code, or wrap in a try/catch, and return null or String.Empty if it fails? – Phil Apr 22 '20 at 10:47
  • https://stackoverflow.com/questions/329307/how-to-get-website-title-from-c-sharp – BlackHole Apr 22 '20 at 10:50
  • tried all from that theme but as I said, I successfuly get title from most of web site, but accidentally tried this page and raise exception on line where web client try to download string from url which I pass to it – cole Apr 22 '20 at 10:53

1 Answers1

2

If you want to download html and test regex, you can use below code snippet.

    /*usage:*/
//HttpGet(new Uri("https://www.modbee.com/opinion/letters-to-the-editor/article111712867.html"));

public static string HttpGet(Uri uri) {
 HttpWebRequest request = (HttpWebRequest) WebRequest.Create(uri);
 request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

 using(HttpWebResponse response = (HttpWebResponse) request.GetResponse())
 using(Stream stream = response.GetResponseStream())
 using(StreamReader reader = new StreamReader(stream)) {
  var str = reader.ReadToEnd();
  Regex reg = new Regex("<title>(.*)</title>");
  MatchCollection m = reg.Matches(str);
  if (m.Count > 0) {
   return m[0].Value.Replace("<title>", "").Replace("</title>", "");
  } else
   return "";
 }
}
anilcemsimsek
  • 803
  • 10
  • 21