using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
namespace Ex09_8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void getstr(string strUrl)
        {
            string d = @"<title>(?<title>[^<]*)</title>";
            textBox2.Text = Regex.Match(strUrl, d).ToString();

        }

        public bool ValidateDate1(string input)
        {
            return Regex.IsMatch(input, "http(s)?://([\\w-]+\\.)+[\\w-]+(//[\\w- .//?%&=]*)?");
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            { MessageBox.Show("请输入网址"); return; }
            else
            {
                if (ValidateDate1(textBox1.Text.TrimEnd()))
                {
                    string strl;//存储编码
                    WebRequest wb = WebRequest.Create(textBox1.Text.Trim());//请求资源
                    WebResponse webRed = wb.GetResponse();//响应请求
                    Stream redweb = webRed.GetResponseStream();//返回数据存入流中
                    StreamReader sr = new StreamReader(redweb, Encoding.Default);//从流中读出数据
                    StringBuilder sb = new StringBuilder();//可变字符
                    while ((strl = sr.ReadLine()) != null)
                    {
                        sb.Append(strl);//读出数据存入可变字符中
                    }
                    getstr(sb.ToString());//调用正则表达式方法读出标题
                }
                else
                {
                    MessageBox.Show("请输入正确的网址");
                    return;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
            Application.Exit();
        }
    }
}
