我有一个任务是在 C# 上编写一个程序,它可以从网站上找到所有 http 链接。现在我为它写了一个这样的函数:
async static void DownloadWebPage(string url)
{
using (HttpClient client = new HttpClient())
using (HttpResponseMessage response = await client.GetAsync(url))
using (HttpContent content = response.Content)
{
string[] resArr;
string result = await content.ReadAsStringAsync();
resArr = result.Split(new string[] {"href"}, StringSplitOptions.RemoveEmptyEntries);//splitting
//here must be some code-string which finds all neccessary http-links from resArr
Console.WriteLine("Main page of " + url + " size = " + result.Length.ToString());
}
}
使用这个函数,我将网页内容加载到字符串中,然后我解析这个字符串并将结果写入数组,使用“href”-splitter,然后我检查字符串上的每个数组单元,其中包含“href”子字符串。所以我可以获得字符串,其中包含 http 链接。问题开始于字符串拆分时,因为无法找到 http 链接,在我看来这是由于该字符串的内容格式。如何解决?