zoukankan      html  css  js  c++  java
  • 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}
  • 相关阅读:
    【洛谷P1330】封锁阳光大学
    【洛谷P1087】FBI树
    hdu 4504(动态规划)
    hdu 4503(数学,概率)
    hdu 5400(思路题)
    hdu 5701(区间查询思路题)
    hdu 4502(DP)
    hdu 1401(单广各种卡的搜索题||双广秒速)
    hdu 1258(DFS)
    hdu 1254(搜索题)
  • 原文地址:https://www.cnblogs.com/gygang/p/8950143.html
Copyright © 2011-2022 走看看