• 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}
  • 相关阅读:
    二分查找法
    Three-way Partition
    百面机器学习读书笔记
    天才在左,疯子在右
    Coach Shane's Daily English Dictaion 6-10
    Coach Shane's Daily English Dictation 1-5
    国外有意思的网站
    docker操作指南
    创建docker本地仓库的步骤
    tensorflow去掉warning的方法
  • 原文地址:https://www.cnblogs.com/gygang/p/8950143.html
Copyright © 2020-2023  润新知