Asp.net 如何接收post过来的 json 数据

 1 <%@ WebHandler Language="C#" Class="Handler" %>
 2 using System;
 3 using System.Web;
 4 public class Handler : IHttpHandler 
 5 {
 6     //服务端
 7     public void ProcessRequest (HttpContext context) {
 8         context.Response.ContentType = "application/json";
 9         context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
10         using (var reader = new System.IO.StreamReader(context.Request.InputStream))
11         {
12             String xmlData = reader.ReadToEnd();
13 
14             if (!string.IsNullOrEmpty(xmlData))
15             {
16                          //业务处理
17              }
18         }
19     }
20     public bool IsReusable {
21         get {
22             return false;
23         }
24     }
25 }
 1 
//客户端
     private string HttpPost(string Url, string postDataStr) 2 { 3 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); 4 request.Method = "POST"; 5 request.ContentType = "application/json"; 6 request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr); 7 Stream myRequestStream = request.GetRequestStream(); 8 StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312")); 9 myStreamWriter.Write(postDataStr); 10 myStreamWriter.Close(); 11 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 12 Stream myResponseStream = response.GetResponseStream(); 13 StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); 14 string retString = myStreamReader.ReadToEnd(); 15 myStreamReader.Close(); 16 myResponseStream.Close(); 17 return retString; 18 }


<%@ WebHandler Language="C#" Class="Handler" %>using System;using System.Web;public class Handler : IHttpHandler {        public void ProcessRequest (HttpContext context) {        context.Response.ContentType = "application/json";        context.Response.Cache.SetCacheability(HttpCacheability.NoCache);        using (var reader = new System.IO.StreamReader(context.Request.InputStream))        {            String xmlData = reader.ReadToEnd();
            if (!string.IsNullOrEmpty(xmlData))            {                         //业务处理             }        }    }    public bool IsReusable {        get {            return false;        }    }}

猜你喜欢

转载自www.cnblogs.com/gygang/p/8950143.html