---
title: "抓取http网页的全部链接"
author: "jandroid"
date: "2007-06-04T12:11:00Z"
tags: []
slug: "http"
draft: false
lastmod: "2011-11-14T09:35:41Z"
id: 1257406
url: "https://jandroid.iteye.com/blog/1257406"
---

**Asp.net中抓取网页的全部链接**

**效果图：**

![](http://p.blog.csdn.net/images/p_blog_csdn_net/cjjky/1.jpg)

**后台代码实现：**

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Collections;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.ComponentModel;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Data;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Drawing;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Web;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Web.SessionState;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Web.UI;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Web.UI.WebControls;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Web.UI.HtmlControls;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Net;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.IO;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Collections;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Text.RegularExpressions;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)namespacegetwebsite  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*////&lt;summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///WebForm1的摘要说明。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)///&lt;/summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicclassWebForm1:System.Web.UI.Page  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)protectedSystem.Web.UI.WebControls.TextBoxTextBox1;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)protectedSystem.Web.UI.WebControls.ButtonButton1;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)protectedSystem.Web.UI.WebControls.TextBoxTextBox2;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)protectedSystem.Web.UI.WebControls.RegularExpressionValidatorRegularExpressionValidator1;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatevoidPage\_Load(objectsender,System.EventArgse)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(!this.IsPostBack)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//在此处放置用户代码以初始化页面  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)Web窗体设计器生成的代码#regionWeb窗体设计器生成的代码  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)overrideprotectedvoidOnInit(EventArgse)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//CODEGEN:该调用是ASP.NETWeb窗体设计器所必需的。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)InitializeComponent();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)base.OnInit(e);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*////&lt;summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///设计器支持所需的方法-不要使用代码编辑器修改  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///此方法的内容。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)///&lt;/summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatevoidInitializeComponent()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.Button1.Click+=newSystem.EventHandler(this.Button1\_Click);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.Load+=newSystem.EventHandler(this.Page\_Load);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)#endregion  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatevoidButton1\_Click(objectsender,System.EventArgse)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.TextBox2.Text="";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)stringweb\_url=this.TextBox1.Text;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)stringall\_code="";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)HttpWebRequestall\_codeRequest=(HttpWebRequest)WebRequest.Create(web\_url);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)WebResponseall\_codeResponse=all\_codeRequest.GetResponse();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)StreamReadersr=newStreamReader(all\_codeResponse.GetResponseStream());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)all\_code=sr.ReadToEnd();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)sr.Close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ArrayListmy\_list=newArrayList();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)stringp=@"http://(\[w-]+.)+\[w-]+(/\[w-./?%&=]\*)?";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Regexre=newRegex(p,RegexOptions.IgnoreCase);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)MatchCollectionmc=re.Matches(all\_code);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)for(inti=0;i&lt;=mc.Count-1;i++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)bool\_foo=false;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)stringname=mc\[i].ToString();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)foreach(stringlistinmy\_list)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(name==list)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\_foo=true;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)break;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}//过滤  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(!\_foo)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)TextBox2.Text+=name+" ";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}