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

namespace GxPress.Common.Http
{
    /// <summary>
    /// HttpClient
    /// </summary>
    public static class HttpClientHelper
    {
        /// <summary>
        /// get
        /// </summary>
        /// <param name="url"></param>
        /// <param name="headers"></param>
        /// <returns></returns>
        public static async Task<HttpResponseMessage> GetHeadersAsync(string url, Dictionary<string, string> headers)
        {
            HttpClient httpClient = new HttpClient(); //http对象
            //添加header
            foreach (var header in headers)
            {
                httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
            }
            var response = await httpClient.GetAsync(url);
            return response;
        }
        /// <summary>
        /// 根据地址下载
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static async Task<byte[]> GetByteArrayAsync(string url)
        {
            HttpClient httpClient = new HttpClient(); //http对象
            var response = await httpClient.GetByteArrayAsync(url);
            return response;
        }

        /// <summary>
        /// post
        /// </summary>
        /// <param name="url"></param>
        /// <param name="jsonObj">json</param>
        /// <param name="headers"></param>
        /// <returns></returns>
        public static async Task<HttpResponseMessage> PostAsync(string url, object jsonObj,
            Dictionary<string, string> headers = null)
        {
            HttpClient httpClient = new HttpClient(); //http对象
            if (headers != null)
                foreach (var header in headers)
                {
                    httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
                }
            HttpResponseMessage response = await httpClient.PostAsync(url,
                new JsonContent(jsonObj));
            return response;
        }

        /// <summary>
        /// rest删除
        /// </summary>
        /// <param name="url"></param>
        /// <param name="headers"></param>
        /// <returns></returns>
        public static async Task<HttpResponseMessage> DeleteAsync(string url, Dictionary<string, string> headers = null)
        {
            HttpClient httpClient = new HttpClient(); //http对象
            if (headers != null)
                foreach (var header in headers)
                {
                    httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
                }

            HttpResponseMessage response = await httpClient.DeleteAsync(url);
            return response;
        }

        /// <summary>
        /// 返回JSon数据
        /// </summary>
        /// <param name="JSONData">要处理的JSON数据</param>
        /// <param name="Url">要提交的URL</param>
        /// <returns>返回的JSON处理字符串</returns>
        public static string GetResponseData(string JSONData, string Url)
        {
            try
            {
                byte[] bytes = Encoding.UTF8.GetBytes(JSONData);
                var request = (HttpWebRequest)WebRequest.Create(Url);
                request.Method = "POST";
                request.ContentLength = bytes.Length;
                request.ContentType = "text/xml";
                Stream reqstream = request.GetRequestStream();
                reqstream.Write(bytes, 0, bytes.Length);

                //声明一个HttpWebRequest请求
                request.Timeout = 90000;
                //设置连接超时时间
                //request.Headers.Set("Pragma", "no-cache");
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream streamReceive = response.GetResponseStream();
                Encoding encoding = Encoding.UTF8;

                StreamReader streamReader = new StreamReader(streamReceive, encoding);
                string strResult = streamReader.ReadToEnd();
                streamReceive.Dispose();
                streamReader.Dispose();

                return strResult;
            }
            catch (System.Exception)
            {

                return "";
            }
            return "";
        }
    }
}
