56、C# HttpPost 通用类,支持后台携带Token令牌信息以及通过网关请求微服务接口

时间:2025-03-19 07:39:32
    class HttpPost
    {
        public class Types
        {
            public static string JSON { get { return "json"; } }

            public static string X_WWW_FORM_URLENCODED { get { return "x-www-form-urlencoded"; } }
        }

        public static string Request(string url, string data, string type)
        {
            if ((type))
            {
                type = Types.X_WWW_FORM_URLENCODED;
            }
            
            UTF8Encoding encoding = new UTF8Encoding();

            byte[] bytesToPost = (data); //转换为bytes数据

            string responseResult = ;
            HttpWebRequest req = null;
            HttpWebResponse cnblogsRespone = null;
            try
            {
                req = (HttpWebRequest)(url);
                 = "POST";
                 = "application/" + type + ";charset=utf-8";
                 = ;

                // 解决通过网关请求微服务接口无法获取返回数据的问题
                .Expect100Continue = false; 
                 = HttpVersion.Version11;

                using (Stream reqStream = ())
                {
                    (bytesToPost, 0, );
                }
                cnblogsRespone = (HttpWebResponse)();
                if (cnblogsRespone != null &&  == )
                {
                    StreamReader sr;
                    using (sr = new StreamReader(()))
                    {
                        responseResult = ();
                    }
                    ();
                }
                return responseResult;
            }
            catch (Exception ex)
            {
                (ex);
                return null;
            }
            finally
            {
                if (cnblogsRespone != null)
                {
                    ();
                }

            }
        }

        public static string RequestWithToken(string url, string data, string type, String token)
        {
            if ((type))
            {
                type = Types.X_WWW_FORM_URLENCODED;
            }
            UTF8Encoding encoding = new UTF8Encoding();

            byte[] bytesToPost = (data); //转换为bytes数据

            string responseResult = ;
            HttpWebRequest req = null;
            HttpWebResponse cnblogsRespone = null;
            try
            {
                req = (HttpWebRequest)(url);
                 = "POST";
                 = "application/" + type + ";charset=utf-8";
                 = ;
                ("Authorization", token);

                // 解决通过网关请求微服务接口无法获取返回数据的问题
                .Expect100Continue = false; 
                 = HttpVersion.Version11;

                using (Stream reqStream = ())
                {
                    (bytesToPost, 0, );
                }
                cnblogsRespone = (HttpWebResponse)();
                if (cnblogsRespone != null &&  == )
                {
                    StreamReader sr;
                    using (sr = new StreamReader(()))
                    {
                        responseResult = ();
                    }
                    ();
                }
                return responseResult;
            }
            catch (Exception ex)
            {
                (ex);
                return null;
            }
            finally
            {
                if (cnblogsRespone != null)
                {
                    ();
                }

            }
        }
    }