Trying to get the title of a website URL entered by the user in a textbox but I am not sure what to put in place of "source".
String title = Regex.Match(source, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;
label2.Text = title;
main portion of my code
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(this.textBox1.Text);
using (HttpWebResponse myHttpWebResponse = (HttpWebResponse)myRequest.GetResponse())
{
if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)
{
string message = "200 OK";
string caption = "Status Code";
MessageBoxButtons buttons = MessageBoxButtons.OK;
DialogResult result;
result = MessageBox.Show(message, caption, buttons);
Stream streamResponse = myHttpWebResponse.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
Char[] readBuffer = new Char[256];
int count = streamRead.Read(readBuffer, 0, 256);
while (count > 0)
{
String resultData = new String(readBuffer, 0, count);
richTextBox1.Text += (resultData);
label3.ForeColor = System.Drawing.Color.Green;
label3.Text = " 200 OK ";
label3.Visible = true;
label2.Visible = true;
count = streamRead.Read(readBuffer, 0, 256);
}
streamRead.Close();
streamResponse.Close();
myHttpWebResponse.Close();
}
}
}
catch
{
if (textBox1.Text != (@" ^(\b(https):(\/\/|\\\\)[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?|\bwww\.[^\s])"))
{
string message2 = "Status Code";
string caption2 = "404 Not Found";
MessageBoxButtons buttons2 = MessageBoxButtons.OK;
DialogResult result2;
result2 = MessageBox.Show(message2, caption2, buttons2);
richTextBox1.Clear();
richTextBox1.Text = "Page not found!";
label3.Text = "Error 404 ";
label3.ForeColor = System.Drawing.Color.Red;
label3.Visible = true;
}
}
}
What I have tried:
String title = Regex.Match(textBox1.Text, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;
label2.Text = title;
did not work. what can I change?