繁体   English   中英

如何使用 ASP.NET 4.0 Web 表单获取原始请求正文

[英]How do I get raw request body using ASP.NET 4.0 Web Form

我正在尝试在 ASP.NET 4.0 WebForm 中获取原始请求正文。
Request["param"]、Request.Form["param"]、Request.QueryString["param"] 不工作。
知道如何获取此参数吗?

在此处输入图像描述

// API POST Request with this Request Body (raw-json)
    {
      "name" : "Apple",
      "image" : "https://upload.wikimedia.org/wikipedia/commons/thumb/1/15/Red_Apple.jpg/265px-Red_Apple.jpg",
      "price" : 35
    }

// API Server trying to get request body.

// didn't work. empty
Request["name"]
// didn't work. empty
Request.Form["name"]
// didn't work. empty
Request.QueryString["name"]

您可以使用Request.InputStream

// include this in the top of your page to use JavaScriptSerializer and Hashtable
using System.Web.Script.Serialization;
using System.Collections;

...
using (var sr = new StreamReader(Request.InputStream))
{
    string body = sr.ReadToEnd();
    
    // Deserialize JSON to C# object
    // you can use some modern libs such as Newtonsoft JSON.NET instead as well
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    Hashtable hashtable = serializer.Deserialize<Hashtable>(body);

    string name = hashtable["name"].ToString();
    string image = hashtable["image"].ToString();
    string price = hashtable["price"].ToString();

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM