using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
1.先取得网页的原代码
HttpWebRequest request=(HttpWebRequest) WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader sr = new StreamReader(stream);
string str=sr.ReadToEnd();
sr.Close();
stream.Close();
response.Close();
得到网页的html源代码以后。再根据源代码分析 所有 <a href ="url"> 最后得到 href后面 url的链接地址
正则表达式
|(?<1>[/\a-z0-9_]\S*))(\s[^>]*)?>(?<2>.*?)</a>", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
循环读出 连接地址
{
TextBox1.Text+= m.Groups[1].ToString()+"\n";
}
运行后
TextBox1 将显示分析后的所有网页的连接 :
http://www.dotlucene.net/
http://www.castleproject.org/
http://www.codeplex.com/
http://www.codeproject.com/
http://www.asp.net/
http://www.nhibernate.org/
http://www.blogjava.net/wujun/CommentsRSS.aspx
http://www.blogjava.net/wujun/archive/2006/10/23/47150.html#76745
http://www.blogjava.net/wujun/archive/2006/10/23.html
http://www.blogjava.net/wujun/archive/2006/10/23/76769.html
http://www.blogjava.net/wujun/archive/2006/10/23/76769.html
http://www.blogjava.net/wujun/archive/2006/10/23/76769.html#FeedBack
http://www.blogjava.net/wujun/admin/EditPosts.aspx?postid=76769
http://www.blogjava.net/wujun/AddToFavorite.aspx?id=76769
http://www.blogjava.net/wujun/archive/2006/10/20.html
......
..............
.........................等等等。。。
/// C# :从一段字符串中,输入开始和结束的字符,取中间的字符
/// </summary>
/// <param name="str">一段字符串</param>
/// <param name="strStart">开始字符</param>
/// <param name="strEnd">结束字符</param>
/// <returns></returns>
public static string AnalyzeMessage(string str, string strStart, string strEnd)
{
string Result = "";
int i = str.IndexOf(strStart);
if (i >= 0)
{
int j = str.IndexOf(strEnd, i + strStart.Length);
if (j > 0)
{
Result = str.Substring(i + strStart.Length, j - i - strStart.Length);
}
}
return Result;
}