![](/img/trans.png)
[英]How to get the raw request body in an asp.NET 3.5 WCF Web Service
[英]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.