在C#中通过url获取页面HTML文档

这个简单直接上代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System;
using System.Net.Http;
using System.Threading.Tasks;

class Program
{
static async Task Main()
{
Console.WriteLine("输入url地址:");
string url = Console.ReadLine();
using (HttpClient client = new HttpClient())
{
try
{
HttpResponseMessage response = await client.GetAsync(url);
string content = await response.Content.ReadAsStringAsync();
Console.WriteLine(content);
}
catch (HttpRequestException HRex)
{
Console.WriteLine("请求失败: " + HRex.Message);
}
catch (Exception ex)
{
Console.WriteLine("请求失败:" + ex.Message);
}
}
Console.ReadLine();
}
}


你猜的没错,这篇就是凑数的