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

namespace PushPlatform.Common.Util
{
    public class WebApiUtil
    {
        /// <summary>
        /// 私有构造方法，防止实例化
        /// </summary>
        private WebApiUtil()
        {

        }

        /// <summary>
        /// 单例模式
        /// </summary>
        private static WebApiUtil _instance = null;

        public static WebApiUtil Instance()
        {
            if (_instance == null)
            {
                _instance = new WebApiUtil();
            }
            return _instance;
        }

        #region 获取响应结果
        /// <summary>
        /// 获取响应结果
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="param">请求参数</param>
        /// <param name="method">请求方式</param>
        /// <returns></returns>
        public string GetResponse(string url, string param, string method)
        {
            string reStr = "";
            try
            {
                if (string.IsNullOrEmpty(url)) throw new Exception("请求地址不能为空！");
                if (string.IsNullOrEmpty(param)) throw new Exception("请求参数不能为空！");
                method = method ?? "GET";
                if (method.ToUpper() == "GET")
                {
                    if (url.EndsWith("?"))
                        url = url + param;
                    else
                        url = url + "?" + param;
                }
                HttpWebRequest hwrq = (HttpWebRequest)WebRequest.Create(url);
                hwrq.Method = method.ToUpper();
                hwrq.KeepAlive = true;
                if (method.ToUpper() == "POST")
                {
                    ServicePointManager.Expect100Continue = false;
                    hwrq.ContentType = "application/x-www-form-urlencoded";

                    var enc = Encoding.GetEncoding("utf-8");

                    byte[] bs = enc.GetBytes(param);

                    hwrq.ContentLength = bs.Length;
                    using (Stream reqStream = hwrq.GetRequestStream())
                    {
                        reqStream.Write(bs, 0, bs.Length);
                    }
                }
                hwrq.Timeout = 50000;
                using (HttpWebResponse hwrp = (HttpWebResponse)hwrq.GetResponse())
                {

                    while (hwrp.StatusCode != HttpStatusCode.OK)
                    {
                        System.Threading.Thread.Sleep(500);
                    }
                    Stream st = hwrp.GetResponseStream();

                    reStr = new StreamReader(st, Encoding.UTF8).ReadToEnd();
                    st.Close();
                    st.Dispose();
                }
            }
            catch (Exception ex)
            {
                reStr = ex.Message;
            }
            return reStr;
        }
        #endregion

        #region 获取参数字符串
        /// <summary>
        /// 获取参数字符串
        /// </summary>
        /// <param name="source">参数对象</param>
        /// <returns></returns>
        public static string GetParamStr(object source)
        {
            string param = string.Empty;

            var sPropertys = source.GetType().GetProperties();

            foreach (var sp in sPropertys)
            {
                var name = sp.Name;

                var value = sp.GetValue(source, null);

                param += name + "=" + value + "&";
            }

            param = param.Remove(param.LastIndexOf('&'), 1);

            return param;
        }
        #endregion
    }
}
