winform截取网页邮箱

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static String GetWebContent(String Url, Encoding encode)
        {
            try
            {
                if (Url.Equals("about:blank")) return null;
                if (!Url.StartsWith("http://") && !Url.StartsWith("https://")) { Url = "http://" + Url; }
                //HttpWebRequest和HttpWebResponse类是用于发送和接收HTTP数据的最好选择。
                //它们支持一系列有用的属性。这两个类位 于System.Net命名空间,
                //默认情况下这个类对于控制台程序来说是可访问的。
                //请注意,HttpWebRequest对象不是利用new关键字通过构 造函数来创建的,而是利用工厂机制(factory mechanism)通过Create()方法来创建的。  
                HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(Url); //根据获取网址向服务器发送请求:             
                myReq.Timeout = 60000;//获取或设置 GetResponse 和 GetRequestStream 方法的超时值(以毫秒为单位)。 (重写 WebRequest.Timeout。)
                myReq.Accept = "Accept-Language:zh-cn";//支持语言
                myReq.KeepAlive = true;//获取或设置一个值,该值指示是否与 Internet 资源建立持久性连接,获取或设置一个值,该值指示是否在导航历史记录中保留 Page 实例
                myReq.Referer = Url;//获取或设置 Referer HTTP 标头的值。
                myReq.MaximumAutomaticRedirections = 100;//获取或设置请求将跟随的重定向的最大数目
                myReq.AllowAutoRedirect = true;//网络响应请求,获取或设置一个值,该值指示请求是否应跟随重定向响应
                
                HttpWebResponse myres = (HttpWebResponse)myReq.GetResponse();
                
                Stream WebStream = myres.GetResponseStream();//获取网页字符流,并最终转换为系统默认的字符:
                StreamReader Sreader = new StreamReader(WebStream, encode);
                String HtmlText = Sreader.ReadToEnd();
                
                Sreader.Close();//关闭字符流:
                WebStream.Close();
                return HtmlText;
            }
            catch (Exception ex)
            {
                MessageBox.Show("获取网站信息错误:" + ex.Message);

                return null;
            }
        }
        



        private void button1_Click_1(object sender, EventArgs e)
        {
            try
            {
                string x = GetWebContent(textBox1.Text, Encoding.UTF8);
                Regex r = new Regex(@"w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*");//正则 


                System.Text.RegularExpressions.MatchCollection mc = r.Matches(x);//表示通过以迭代方式将正则表达式模式应用于输入字符串所找到的成功匹配的集合。
                //该集合为不可变(只读)的,并且没有公共构造函数。 Regex.Matches 方法返回 MatchCollection 对象。
                //集合中包含零个或多个 System.Text.RegularExpressions.Match 对象。
                //如果匹配成功,则将使用在输入字符串中找到的每个匹配对应的一个 System.Text.RegularExpressions.Match 对象来计算集合。 
                //如果匹配不成功,则集合不包含 System.Text.RegularExpressions.Match 对象,其 Count 属性等于 0。

                for (int i = 0; i < mc.Count; i++)
                {
                    if (mc.Count == 0)
                    {
                        textBox2.Text = "未查询到符合邮箱地址的信息。";
                    }
                    else
                    {
                        textBox2.Text += mc[i].Value + "
";
                    }
                }
            }
            catch (Exception)
            {


            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string x = GetWebContent(textBox1.Text, Encoding.UTF8);
            textBox2.Text = x;
        }
    }
}

  
原文地址:https://www.cnblogs.com/dlexia/p/4649072.html