【C#】Post Json数据格式到 Url,Json序列化

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/zhaoyuqings/article/details/83418614
        /// <summary>
        /// 上传Json格式的数据到url地址
        /// </summary>
        /// <param name="url"></param>
        /// <param name="josn"></param>
        /// <returns>Post返回</returns>
        public string PostJson(string url, string josn)
        {
            //log.Debug(url);
            //log.Debug(josn);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/json";
            //声明一个HttpWebRequest请求
            request.Timeout = 80000;
            //转发机制
            Encoding encoding = Encoding.UTF8;
            Stream streamrequest = request.GetRequestStream();
            StreamWriter streamWriter = new StreamWriter(streamrequest, encoding);
            streamWriter.Write(josn);
            streamWriter.Flush();
            streamWriter.Close();
            streamrequest.Close();

            //设置连接超时时间
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream streamresponse = response.GetResponseStream();
            StreamReader streamReader = new StreamReader(streamresponse, encoding);
            string result = streamReader.ReadToEnd();
            //log.InfoFormat("上传返回信息,{0}", result);
            streamresponse.Close();
            streamReader.Close();

            return result;
        }
        /// <summary>
        /// 指定Post地址使用Get 方式获取全部字符串
        /// </summary>
        /// <param name="url">请求后台地址</param>
        /// <returns></returns>
        public static string Post(string url, Dictionary<string, string> dic)
        {
            string result = "";
            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++;
            }
            string s = url + "?" + builder.ToString();
            log.Info("Url " + s);

            return Post(s);

        }

        /// <summary>
        /// 指定Post地址使用Get 方式获取全部字符串
        /// </summary>
        /// <param name="url">请求后台地址</param>
        /// <returns></returns>
        public static string Post(string url)
        {
            string result = "";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Stream stream = resp.GetResponseStream();
            //获取内容
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
            return result;
        }


        /// <summary>
        /// Post application/x-www-form-urlencoded
        /// </summary>
        /// <param name="url">请求后台地址</param>
        /// <returns></returns>
        public static string Post2(string url, Dictionary<string, string> dic)
        {
            string result = "";
            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++;
            }
            string s = builder.ToString();
            log.Debug(s);

            byte[] bs = Encoding.UTF8.GetBytes(s);
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = bs.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(bs, 0, bs.Length);
                reqStream.Close();
            }
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Stream stream = resp.GetResponseStream();
            //获取内容
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
            return result;
        }
    
    //Json序列化和反序列化,引用.Net System.Web.Extensions
    public class ResponseInfo
    {
        /// <summary>
        /// 返回码 
        /// </summary>
        public string code { get; set; }

        /// <summary>
        /// 具体信息
        /// </summary>
        public string msg { get; set; }

       
        //Json序列化
        public string Serialize()
        {
            System.Web.Script.Serialization.JavaScriptSerializer serialze = new System.Web.Script.Serialization.JavaScriptSerializer();
            string s = serialze.Serialize(this);
            return s;
        }
        
        //Json反序列化
        public static ResponseInfo DeSerialize(string s)
        {
            System.Web.Script.Serialization.JavaScriptSerializer serialze = new System.Web.Script.Serialization.JavaScriptSerializer();
            return serialze.Deserialize<ResponseInfo>(s);
        }
    }

 

展开阅读全文

没有更多推荐了,返回首页