📄 linkextract.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.IO;
public partial class LinkExtract : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnExtract_click(object sender, EventArgs e)
{
string rawCode = DownloadData(txtURL.Text);
List<string> links=ExtractLinks(rawCode);
int count = 0;
txtLinks.Items.Clear();
foreach (string link in links)
{
//txtLinks.Text += link + Environment.NewLine;
txtLinks.Items.Add(link.ToString());
count++;
}
//Show how many links there are
lCount.Text = count.ToString() + " Link(s) Found";
}
private List<string> ExtractLinks(string rawCode)
{
List<string> links = new List<string>();
string startSquence = "<a href=\"";
string endSequence = "\"";
rawCode = rawCode.ToLower();
while (rawCode.IndexOf("<a href") != -1)
{
int start = rawCode.IndexOf(startSquence) + startSquence.Length;
int end = rawCode.IndexOf(endSequence, start);
//Extract the link, and add it to the list
if (end > start)
{
string link = rawCode.Substring(start, end - start);
if (link != string.Empty)
{
if (!link.StartsWith("http://"))
{
//It's a relative link, add a ..
link = "../" + link;
}
links.Add(link);
}
}
//Trim the raw data
rawCode = rawCode.Substring(end + endSequence.Length);
}
return links;
}
private string DownloadData(string url)
{
byte[] downloadedData = new byte[0];
try
{
//Get a data stream from the url
WebRequest req = WebRequest.Create(url);
WebResponse response = req.GetResponse();
Stream stream = response.GetResponseStream();
//Download in chuncks
byte[] buffer = new byte[1024];
//Get Total Size
int dataLength = (int)response.ContentLength;
//Download to memory
MemoryStream memStream = new MemoryStream();
while (true)
{
//Try to read the data
int bytesRead = stream.Read(buffer, 0, buffer.Length);
//Avoid application freezing
//Application.DoEvents();
if (bytesRead == 0)
{
//Finished downloading
break;
}
else
{
//Write the downloaded data
memStream.Write(buffer, 0, bytesRead);
}
}
//Convert the downloaded stream to a byte array
downloadedData = memStream.ToArray();
//Clean up
stream.Close();
memStream.Close();
}
catch (Exception)
{
//May not be connected to the internet
//Or the URL might not exist
//txtLinks.Items.Clear();
Response.Write("There was an error accessing the URL.");
}
//Convert the data into string
return ASCIIEncoding.ASCII.GetString(downloadedData);
}
protected void txtLinks_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -