HttpClient的使用 (dotnetcore) (.netcore)

版权声明:版权声明:本文为博主原创文章,转载请附上博文链接! https://blog.csdn.net/qq_36051316/article/details/84380024

dotnetcore使用HttpClient

dotnetcore使用HttpClient进行get请求获取dom

我们可以参考官网:https://docs.microsoft.com/zh-cn/dotnet/api/system.net.http.httpclient?view=netcore-2.1

需要引用:using System.Net.Http;

使用语言:C#

环境:.net core 2.1 (当前使用)

核心代码:

HttpClient client = new HttpClient(new HttpClientHandler());
var html = client.GetStringAsync(“https://blog.csdn.net/qq_36051316/article/details/84380024”).Result.ToString();
System.Console.WriteLine(html);

HttpClient client = new HttpClient(new HttpClientHandler());
var html = client.GetStringAsync("https://blog.csdn.net/qq_36051316/article/details/84380024").Result.ToString();
System.Console.WriteLine(html);

全部代码:

using System;
using System.Net.Http;
namespace netcore.Http_Client.demo
{
    class Program
    {
        static void Main(string[] args)
        {
            HttpClient client = new HttpClient(new HttpClientHandler());
            //获取我们当前网站的dom
            var html = client.GetStringAsync("https://blog.csdn.net/qq_36051316/article/details/84380024").Result.ToString();
            Console.WriteLine("输出网站内容:");
            Console.WriteLine("================华丽分割线==================");
            System.Console.WriteLine(html);
        }
    }
}


请求的结果

猜你喜欢

转载自blog.csdn.net/qq_36051316/article/details/84380024