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

推荐订阅源

G
Google Developers Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
C
Check Point Blog
B
Blog RSS Feed
Y
Y Combinator Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
About on SuperTechFans
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 聂微东
阮一峰的网络日志
阮一峰的网络日志
人人都是产品经理
人人都是产品经理
小众软件
小众软件
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
GbyAI
GbyAI
The Cloudflare Blog
博客园 - 叶小钗
S
SegmentFault 最新的问题
博客园 - Franky
Engineering at Meta
Engineering at Meta
F
Fortinet All Blogs
B
Blog
Jina AI
Jina AI

博客园 - 阿风的博客

高压力下正则表达式的性能瓶颈 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";
            }


        }