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

        /// 
        /// 上传Json格式的数据到url地址
        /// 
        /// 
        /// 
        /// Post返回
        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;
        }
        /// 
        /// 指定Post地址使用Get 方式获取全部字符串
        /// 
        /// 请求后台地址
        /// 
        public static string Post(string url, Dictionary 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);

        }

        /// 
        /// 指定Post地址使用Get 方式获取全部字符串
        /// 
        /// 请求后台地址
        /// 
        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;
        }


        /// 
        /// Post application/x-www-form-urlencoded
        /// 
        /// 请求后台地址
        /// 
        public static string Post2(string url, Dictionary 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
    {
        /// 
        /// 返回码 
        /// 
        public string code { get; set; }

        /// 
        /// 具体信息
        /// 
        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(s);
        }
    }

 

你可能感兴趣的:(C#基础)