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 }
26 
27      //客户端
28 private string HttpPost(string Url, string postDataStr)
29 {
30     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
31     request.Method = "POST";
32     request.ContentType = "application/json";
33     request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
34     Stream myRequestStream = request.GetRequestStream();
35     StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
36     myStreamWriter.Write(postDataStr);
37     myStreamWriter.Close();
38     HttpWebResponse response = (HttpWebResponse)request.GetResponse();
39     Stream myResponseStream = response.GetResponseStream();
40     StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
41     string retString = myStreamReader.ReadToEnd();
42     myStreamReader.Close();
43     myResponseStream.Close();
44     return retString;
45}

 

posted @ 2018-04-26 10:49  gygang  阅读(16961)  评论(0编辑  收藏  举报