惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
博客园 - 司徒正美
The GitHub Blog
The GitHub Blog
G
Google Developers Blog
Stack Overflow Blog
Stack Overflow Blog
博客园_首页
Google DeepMind News
Google DeepMind News
博客园 - 【当耐特】
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Recent Announcements
Recent Announcements
aimingoo的专栏
aimingoo的专栏
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Y
Y Combinator Blog
B
Blog RSS Feed
人人都是产品经理
人人都是产品经理
MongoDB | Blog
MongoDB | Blog
量子位
博客园 - Franky
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Cloudflare Blog
有赞技术团队
有赞技术团队
Jina AI
Jina AI
GbyAI
GbyAI

博客园 - 阿风的博客

高压力下正则表达式的性能瓶颈 SQL SERVER2008不在同一局域网内实现订阅发布的方法 得到一个字符串的占位长度的函数 一个类似SPY++雷达手的工具 RPM V2.2发布 关于Table Schema 一段取得数据库中所有表字段及字段中文描述的SQL语句 多主键情况下的真分页存储过程 POP气球机v1.3 c#对调用存储过程的简化 PCPOP多功能外挂v1.1 PCPOP多功能外挂v1.0 POP气球机 关于系统的垃圾文件 关于在同个页面实现多种编码的显示方法 关于几个比较经典的SQL语句 关于容器中的控件的使用 关于文件的上传。 网站开发步骤
网页源代码的获取方法
阿风的博客 · 2005-04-14 · via 博客园 - 阿风的博客

在WinForm里获得一个网页的源代码在有的情况下非常有用,特别是在做外挂的时候。这里用pop为例,讲一下获取的方法,然后顺便通过正则表达式获得用户登陆的验证码。
小程序的演示效果如下:
popValidateCode.jpg
这段是获取HTML源代码的方法:

        private void btnShowCode_Click(object sender, System.EventArgs e)
        
{
            System.Net.WebRequest myWebRequest
=System.Net.WebRequest.Create(this.txtURL.Text);
            myWebRequest.Timeout
=5000;
            
string _htmlCode="";

            
try
            
{
                System.Net.WebResponse myWebR
=myWebRequest.GetResponse();
                System.IO.Stream resStream
=myWebR.GetResponseStream();
                System.IO.StreamReader sr
=new System.IO.StreamReader(resStream,System.Text.Encoding.Default);

                _htmlCode
=sr.ReadToEnd();

                resStream.Close();
                sr.Close();
                
this.txtCode.Text=_htmlCode;
            }

            
catch(System.Net.WebException ex)
            
{
                
this.txtCode.Text=ex.Message;
            }


            getValidateCode(_htmlCode);
        }

通过正则表达式获得其中用户登陆的验证码:

        private void getValidateCode(string htmlCode)
        
{
            
string pattern=@"[>]\d{4}[<]";

            System.Text.RegularExpressions.Regex regex
=new System.Text.RegularExpressions.Regex(pattern);
            System.Text.RegularExpressions.Match match
=regex.Match(htmlCode);

            
if(match.Success)
            
{
                
this.txtValidateCode.Text=match.Value.Substring(1,4);
            }

            
else
            
{
                
this.txtValidateCode.Text="null";
            }


        }