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

namespace ICSSoft.Common
{
    public class HTTPHelper
    {
        private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        //public static string HttpPost(string apiName, string url, string body)
        //{
        //    try
        //    {
        //        //log.Debug(url + Environment.NewLine + body);
        //        Encoding encoding = Encoding.UTF8;
        //        System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
        //        request.Method = "POST";
        //        request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
        //        request.ContentType = "application/json; charset=utf-8";
        //        // request.ContentType = "text/html, application/xhtml+xml";
        //        byte[] buffer = encoding.GetBytes(body);
        //        request.ContentLength = buffer.Length;
        //        request.GetRequestStream().Write(buffer, 0, buffer.Length);
        //        System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
        //        using (System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
        //        {
        //            return reader.ReadToEnd();
        //        }
        //    }
        //    catch (System.Net.WebException ex)
        //    {
        //        log.Error(ex.ToString() + Environment.NewLine + url + Environment.NewLine + body);
        //        throw new Exception(apiName + "调用失败," + ex.Message);
        //    }
        //}

        public static string HttpPost(string apiName, string url, string body)
        {
            try
            {
                Encoding encoding = Encoding.UTF8;
                System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
                request.Method = "POST";
                request.Accept = "application/json, text/javascript, */*";
                request.ContentType = "application/json; charset=utf-8";
                request.Timeout = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["TimeOut"]);//超时时间

                byte[] buffer = encoding.GetBytes(body);
                request.ContentLength = buffer.Length;
                request.GetRequestStream().Write(buffer, 0, buffer.Length);
                System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
                using (System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
                {
                    return reader.ReadToEnd();
                }
            }
            catch (System.Net.WebException ex)
            {
                log.Error(ex.ToString() + Environment.NewLine + url + Environment.NewLine + body);
                throw new Exception(apiName + "调用失败," + ex.Message);
            }
        }
    }
}