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

namespace CallWebApi
{
    public class Post
    {
        public static string Postring(string url, Dictionary<string, string> dic)
        {
            string result = "";
            //url = "http://172.16.22.15:8000/" + url;
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.Proxy = null;

            req.KeepAlive = false;

            #region 添加Post 参数
            StringBuilder builder = new StringBuilder();
            int i = 0;
            foreach (var item in dic)
            {
                if (i > 0)
                    builder.Append("&");
                builder.AppendFormat("{0}={1}", item.Key, item.Value);
                i++;
            }
            byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
            req.ContentLength = data.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            #endregion
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Stream stream = resp.GetResponseStream();

            //获取响应内容
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
            return result;
        }

        public static string Postring1(string url, string token, Dictionary<string, string> dic)
        {
            string results = "";
            //url = "http://172.16.22.15:8000/" + url;

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.Headers.Add("Authorization", "Bearer " + token);
            
            StringBuilder builder = new StringBuilder();
            int i = 0;
            foreach (var item in dic)
            {
                if (i > 0)
                    builder.Append("&");
                builder.AppendFormat("{0}={1}", item.Key, item.Value);
                i++;
            }
            byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
            req.ContentLength = data.Length;
            try
            {
                using (Stream reqStream = req.GetRequestStream())
                {
                    reqStream.Write(data, 0, data.Length);
                    reqStream.Close();
                }
                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                Stream stream = resp.GetResponseStream();
                //获取响应内容
                using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                {
                    results = reader.ReadToEnd();
                }
                return results;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return e.Message;
                throw;
            }

        }
        /// <summary>
        /// 西开电器专用
        /// </summary>
        /// <param name="url"></param>
        /// <param name="token"></param>
        /// <param name="dic"></param>
        /// <returns></returns>
        public static string Post_XK(string url, string token, Dictionary<string, string> dic)
        {
            string results = "";
            //url = "http://172.16.22.15:8000/" + url;

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.Headers.Add("Authorization", "Bearer " + token);
            
            StringBuilder builder = new StringBuilder();
            int i = 0;
            foreach (var item in dic)
            {
                if (i > 0)
                    builder.Append("&");
                builder.AppendFormat("{0}={1}", item.Key, item.Value);
                i++;
            }
            byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
            req.ContentLength = data.Length;
            try
            {
                using (Stream reqStream = req.GetRequestStream())
                {
                    reqStream.Write(data, 0, data.Length);
                    reqStream.Close();
                }
                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                Stream stream = resp.GetResponseStream();
                //获取响应内容
                using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                {
                    results = reader.ReadToEnd();
                }
                return results;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return e.Message;
                throw;
            }

        }


        public static string MyPost()
        {
            // var url = "http://192.168.1.224:41190/";
            var url = "http://127.0.0.1:41190/";
            var controller = "ZSaveTag";
            var action = "SelectPage";
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("OpName","");
            dict.Add("StartTime", "2022-06-24 00:00:00");
            dict.Add("EndTime", "2022-06-30 00:00:00");
            dict.Add("PageCurrent", "1");
            dict.Add("PageSize", "2");


            var reqUrl = url + "/api/" + controller + "/" + action;
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(reqUrl);
            req.Method = "POST";
            req.ContentType = "application/json";
            req.Proxy = null;
            
            req.KeepAlive = false;

            #region 添加Post 参数
            JObject jObject = new JObject();
            jObject.Add("OpName", "");
            jObject.Add("StartTime", "2022-06-24 00:00:00");
            jObject.Add("EndTime", "2022-06-30 00:00:00");
            jObject.Add("PageCurrent", "2");
            jObject.Add("PageSize", "3");
            var jStr = jObject.ToString();

            //StringBuilder builder = new StringBuilder();
            //int i = 0;
            //foreach (var item in dict)
            //{
            //    if (i > 0)
            //        builder.Append("&");
            //    builder.AppendFormat("{0}={1}", item.Key, item.Value);
            //    i++;
            //}
            //byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
            byte[] data = Encoding.UTF8.GetBytes(jStr);
            req.ContentLength = data.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            #endregion
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Stream stream = resp.GetResponseStream();

            string result = "";

            //获取响应内容
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
            return result;
        }

        public static string MyPost(string url,string jsonStr)
        {
            string result = "";

            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "POST";
                req.ContentType = "application/json";
                req.Proxy = null;
                req.KeepAlive = false;

                byte[] data = Encoding.UTF8.GetBytes(jsonStr);
                req.ContentLength = data.Length;
                using (Stream reqStream = req.GetRequestStream())
                {
                    reqStream.Write(data, 0, data.Length);
                    reqStream.Close();
                }

                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                Stream stream = resp.GetResponseStream();


                //获取响应内容
                using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                {
                    result = reader.ReadToEnd();
                }
                Console.WriteLine(result);
                

            }
            catch (Exception err)
            {
                Console.WriteLine("<Error> " + err.Message);
            }

            return result;
        }

    }
}
