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

namespace Fuel.Payment.Service.UnionPayProcessor
{
    public class DefaultClient
    {
        public string Post(string url, string authorization, string parameters, string charset)
        {
            string result = "";
            var encode = Encoding.GetEncoding(charset);
            var req = WebRequest.Create(url) as HttpWebRequest;
            req.Method = "POST";
            req.Timeout = 60000;
            req.ReadWriteTimeout = 60000;
            req.ContentType = "application/json";
            req.Headers.Add("Authorization", authorization);
            #region 添加Post 参数
            byte[] data = encode.GetBytes(parameters);
            req.ContentLength = data.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            #endregion
            HttpWebResponse resp;
            try
            {
                resp = (HttpWebResponse)req.GetResponse();
                Stream stream = resp.GetResponseStream();
            }
            catch (WebException ex)
            {
                resp = (HttpWebResponse)ex.Response;
            }
            using (StreamReader reader = new StreamReader(resp.GetResponseStream(), encode))
            {
                result = reader.ReadToEnd();
            }
            return result;
        }

        public string CreatePostHttpResponse(string url, string authorization, string parameters, string charset)
        {
            //HTTPSQ请求:既支持http请求也支持https请求
            var request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "POST";
            request.Timeout = 60000;
            request.ReadWriteTimeout = 60000;
            request.KeepAlive = true;
            //request.AcceptLanguage = "zh-CN,zh;q=0.8";
            request.ContentType = "application/x-www-form-urlencoded;charset=GBK";
            request.Accept = "application/x-www-form-urlencoded";
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.803.0 Safari/535.1";
            request.ProtocolVersion = HttpVersion.Version10;
            request.Headers.Add("Authorization", authorization);
            //如果需要POST数据
            if (!(parameters == null || parameters == ""))
            {
                //如果用户输入带加号的参数值，需要转义
                byte[] data = Encoding.GetEncoding(charset).GetBytes(parameters);
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }
            }
            var response = request.GetResponse() as HttpWebResponse;
            if (response.StatusCode == HttpStatusCode.OK)
            {
                var sreader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(charset));
                return sreader.ReadToEnd();
            }
            else
            {
                return null;
            }
        }
    }
}
