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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Tailwind CSS Blog
有赞技术团队
有赞技术团队
爱范儿
爱范儿
Engineering at Meta
Engineering at Meta
J
Java Code Geeks
雷峰网
雷峰网
WordPress大学
WordPress大学
L
LangChain Blog
D
DataBreaches.Net
The GitHub Blog
The GitHub Blog
博客园 - 三生石上(FineUI控件)
Microsoft Security Blog
Microsoft Security Blog
P
Proofpoint News Feed
腾讯CDC
GbyAI
GbyAI
罗磊的独立博客
Blog — PlanetScale
Blog — PlanetScale
月光博客
月光博客
F
Fortinet All Blogs
Y
Y Combinator Blog
V
V2EX
A
About on SuperTechFans

博客园 - 阿风的博客

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


        }