using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Nirvana.Common
{
    /// <summary>
    /// 网络请求封装
    /// </summary>
    public class NetRquest
    {
        private IHttpClientFactory _clientFactory;
        public NetRquest(IHttpClientFactory clientFactory)
        {
            _clientFactory = clientFactory;
        }
        /// <summary>
        /// Get请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task<string> GetAsync(string url,string token="")
        {
            var client = _clientFactory.CreateClient();
            //添加请求头
            if (!string.IsNullOrEmpty(token))
            {
                client.DefaultRequestHeaders.Add("token", token);
            }
            client.BaseAddress = new Uri(url);
            //client.DefaultRequestHeaders.Add("Content-Type", "application/json;charset=utf-8");
            var res = await client.GetStringAsync(url);
            return res;
        }
    }
}
