C# 发送POST请求

时间:2025-03-10 09:50:23

    internal static class HttpPost
    {
        private static string GetBody(Dictionary<string, string> senddata)
        {
            if (senddata == null)
            {
                senddata = new Dictionary<string, string>();
            }
            StringBuilder buffer = new StringBuilder();
            int i = 0;
            foreach (string k in )
            {
                if (i > 0)
                {
                    ("&{0}={1}", k, senddata[k]);
                }
                else
                {
                    ("{0}={1}", k, senddata[k]);
                }
                i++;
            }
            string bodys = ();
            return bodys;
        }

        public static string Send(string url, Dictionary<string, string> headerdata, Dictionary<string, string> senddata)
        {
            return Send(url, "POST", headerdata, senddata, "application/x-www-form-urlencoded;charset=UTF-8");
        }

        public static string Send(string url, string method, Dictionary<string, string> headerdata, Dictionary<string, string> senddata)
        {
            return Send(url, method, headerdata, senddata, "application/x-www-form-urlencoded;charset=UTF-8");
        }
        public static string Send(string url, string method, Dictionary<string, string> headerdata, Dictionary<string, string> senddata, string contentType)
        {
            string bodys = GetBody(senddata);
            //new internalErrorLog(new Exception(), "SendBody:" + bodys);
            HttpWebRequest httpRequest = null;
            HttpWebResponse httpResponse = null;

            if (("https://"))
            {
                // = ;
                /*
                 请求某些接口一直返回基础连接已关闭:发送时发生错误
                 远程主机强迫关闭了一个现有的连接
                 */
                 = (SecurityProtocolType)3072;
                ServicePointManager.Expect100Continue = false;
                 = new RemoteCertificateValidationCallback(CheckValidationResult);

                httpRequest = (HttpWebRequest)(new Uri(url));
                 = HttpVersion.Version10;

            }
            else
            {
                httpRequest = (HttpWebRequest)(url);
            }
             = null;
             = method;
            foreach (string k in )
            {
                (k, headerdata[k]);
            }
            //new internalErrorLog(new Exception(), "SendHeader:" + (headerdata));
            //根据API的要求,定义相对应的Content-Type
             = contentType;
            if (0 < )
            {
                byte[] data = Encoding.(bodys);
                using (Stream stream = ())
                {
                    (data, 0, );
                }
            }
            try
            {
                .Expect100Continue = false;
                httpResponse = (HttpWebResponse)();
                Stream st = ();
                StreamReader reader = new StreamReader(st, ("utf-8"));
                string json = ();
                return json;
            }
            catch (WebException ex)
            {
                //new internalErrorLog(ex, "请求URL:" + url);
                httpResponse = (HttpWebResponse);
                return ;
            }
        }

        private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true;
        }

        public static string SendGet(string url, Dictionary<string, string> headerdata)
        {
            HttpWebRequest httpRequest = null;
            HttpWebResponse httpResponse = null;

            if (("https://"))
            {
                 = (SecurityProtocolType)3072;
                ServicePointManager.Expect100Continue = false;
                 = new RemoteCertificateValidationCallback(CheckValidationResult);
                httpRequest = (HttpWebRequest)(new Uri(url));
                 = HttpVersion.Version10;
            }
            else
            {
                httpRequest = (HttpWebRequest)(url);
            }
             = null;
             = "Get";
             = "application/x-www-form-urlencoded;charset=UTF-8";
            foreach (string k in )
            {
                (k, headerdata[k]);
            }
            try
            {
                httpResponse = (HttpWebResponse)();
                Stream st = ();
                StreamReader reader = new StreamReader(st, ("utf-8"));
                string json = ();
                return json;
            }
            catch (WebException ex)
            {
                httpResponse = (HttpWebResponse);
                return ;
            }
        }

        public static string SendJSON(string url, JObject objdata)
        {
            string jsondata = ();
            return SendJSON(url, jsondata);
        }

        public static string SendJSON(string url, string jsondata)
        {
            HttpWebRequest httpRequest = null;
            HttpWebResponse httpResponse = null;

            if (("https://"))
            {
                 = SecurityProtocolType.Ssl3 | ;
                ServicePointManager.Expect100Continue = false;
                 = new RemoteCertificateValidationCallback(CheckValidationResult);
                httpRequest = (HttpWebRequest)(new Uri(url));
                 = HttpVersion.Version10;
                 = false;
                 = 500;
            }
            else
            {
                httpRequest = (HttpWebRequest)(url);
            }
             = null;
             = "POST";
             = "application/json";
            //("", jsondata);
            try
            {
                .Expect100Continue = false;
                byte[] buffer = Encoding.(jsondata);
                //new internalErrorLog(new Exception(jsondata));
                 = ;
                ().Write(buffer, 0, );
                httpResponse = (HttpWebResponse)();
                Stream st = ();
                StreamReader reader = new StreamReader(st, ("utf-8"));
                string json = ();
                return json;
            }
            catch (WebException ex)
            {
                //new internalErrorLog(ex, "发送地址:" + url);
                httpResponse = (HttpWebResponse);
                return ;
            }
        }

        public static string SendJSON(string url, Dictionary<string, string> header, string jsondata)
        {
            HttpWebRequest httpRequest = null;
            HttpWebResponse httpResponse = null;

            if (("https://"))
            {
                 = SecurityProtocolType.Ssl3 | ;
                ServicePointManager.Expect100Continue = false;
                 = new RemoteCertificateValidationCallback(CheckValidationResult);
                httpRequest = (HttpWebRequest)(new Uri(url));
            }
            else
            {
                httpRequest = (HttpWebRequest)(url);
            }
             = "POST";
             = "application/json";
            foreach (string k in )
            {
                (k, header[k]);
            }
            //("", jsondata);
            try
            {
                .Expect100Continue = false;
                byte[] buffer = Encoding.(jsondata);
                //new internalErrorLog(new Exception(jsondata));
                 = ;
                ().Write(buffer, 0, );
                httpResponse = (HttpWebResponse)();
                Stream st = ();
                StreamReader reader = new StreamReader(st, ("utf-8"));
                string json = ();
                return json;
            }
            catch (WebException ex)
            {
                httpResponse = (HttpWebResponse);
                return ;
            }
        }

        public static string Submit(string url, Hashtable ht, string type)
        {
            return Submit(url, ht, type, );
        }

        public static string Submit(string url, NameValueCollection ht, string type)
        {
            return Submit(url, ht, type, );
        }

        public static string Submit(string url, Hashtable ht, string type, Encoding encoding)
        {
            IDictionaryEnumerator enumerator = ();
            NameValueCollection keyValue = new NameValueCollection();
            while (())
            {
                ((), ( == null) ? "" : ());
            }
            return Submit(url, keyValue, type, encoding);
        }

        private static string Submit(string url, NameValueCollection keyValue, string type, Encoding encoding)
        {
            string str = ;
            WebClient client = new WebClient();
            try
            {
                byte[] bytes = (url, (), keyValue);
                str = (bytes);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            finally
            {
                ();
            }
            return str;
        }
    }