How to retrieve JSON via ASP.Net context.Request

后端 未结 5 692
忘掉有多难
忘掉有多难 2020-12-09 17:39
var OrderInfo = {\"ProductID\": 
    \"ProductIDValue\",
    \"ProductName\": \"ProductName\",
    \"Quantity\": 1,
    \"Amount\": 9999,
    \"SLQuantity\": 9999,
          


        
5条回答
  •  自闭症患者
    2020-12-09 17:48

    Digging the Internet. I found out that IE has problem receiving POST request in full. @ronaldwidha's suggestion on InputStream is similar to what I have found. But rather than using javascriptserializer I use JSON.NET Code snippets is below and I hope this would help other with similar problem

     public class JasonHandler : IHttpHandler {
    
     public void ProcessRequest (HttpContext context) {
    
        context.Response.ContentType = "application/json";
        context.Response.ContentEncoding = Encoding.UTF8;
    
        System.IO.Stream body = context.Request.InputStream;
        System.Text.Encoding encoding = context.Request.ContentEncoding;
        System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding);
        if (context.Request.ContentType != null)
        {
            context.Response.Write("Client data content type " + context.Request.ContentType);
        }
        string s = reader.ReadToEnd();
        string[] content = s.Split('&');
        for (int i = 0; i < content.Length; i++)
        {
            string[] fields = content[i].Split('=');
            //context.Response.Write("
    " + fields[0] + "
    "); //context.Response.Write("
    " + fields[1] + "
    "); } string jsonRecord = s; } }

提交回复
热议问题