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

namespace SlMesDbIterface
{
    public partial class MyHttpRequest {

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

            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "POST";
                req.ContentType = "text/html, application/xhtml+xml, 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;
        }

        public static string MyPost(string url,Dictionary<string,string> paramDict, string jsonStr)
        {
            string result = "";

            try
            {
                StringBuilder builder = new StringBuilder();

                builder.Append(url);
                if (paramDict.Count > 0)
                {
                    builder.Append("?");
                    int i = 0;
                    foreach (var item in paramDict)
                    {
                        if (i > 0)
                            builder.Append("&");
                        builder.AppendFormat("{0}={1}", item.Key, item.Value);
                        i++;
                    }
                }

                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(builder.ToString());
                req.Method = "POST";
                req.ContentType = "text/html, application/xhtml+xml, 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;
        }

        public static string MyGet(string url, Dictionary<string,string> paramDict)
        {
            string result = "";

            StringBuilder builder = new StringBuilder();

            builder.Append(url);
            if (paramDict.Count > 0)
            {
                builder.Append("?");
                int i = 0;
                foreach (var item in paramDict)
                {
                    if (i > 0)
                        builder.Append("&");
                    builder.AppendFormat("{0}={1}", item.Key, item.Value);
                    i++;
                }
            }

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(builder.ToString());
            req.Method = "GET";
            req.ContentType = "text/html, application/xhtml+xml, application/json, */*";
            req.Proxy = null;
            req.KeepAlive = false;

            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
            Stream rs = response.GetResponseStream();
            StreamReader sr = new StreamReader(rs, Encoding.UTF8);
            result = sr.ReadToEnd();
            sr.Close();
            rs.Close();

            return result;
        }

    }
}
