简体   繁体   English

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

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

I'm trying to get the raw request body in ASP.NET 4.0 WebForm.我正在尝试在 ASP.NET 4.0 WebForm 中获取原始请求正文。
Request["param"], Request.Form["param"], Request.QueryString["param"] is not working. Request["param"]、Request.Form["param"]、Request.QueryString["param"] 不工作。
Any idea of how to get this parameters?知道如何获取此参数吗?

在此处输入图像描述

// 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"]

You may use Request.InputStream您可以使用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.

相关问题 如何在ASP.NET 3.5 WCF Web服务中获取原始请求正文 - How to get the raw request body in an asp.NET 3.5 WCF Web Service 如何在ASP.NET中获取原始请求主体? - How to get raw request body in ASP.NET? 我如何在ASP.NET Web API中记录原始HTTP请求,而不管它是否路由到控制器? - How do I log the raw HTTP request in ASP.NET Web API whether or not it routes to a controller? 如何从 ASP.NET Web API ValueProvider 中的 HTTP POST 请求检索正文值? - How do I retrieve body values from an HTTP POST request in an ASP.NET Web API ValueProvider? 如何在asp.net 4.0中发布带有Web请求的文件 - how to post files with web request in asp.net 4.0 在ASP.NET Web Api中获取请求正文 - Get body of request in ASP.NET Web Api 如何使用ASP.NET将数据验证添加到Web表单字段? - How do I add data validation to a web form field using ASP.NET? 如何使用ASP.Net Web API创建接受请求正文中任何内容类型的终结点 - How to create an endpoint that accepts any content type in the request body using ASP.Net Web API ASP.NET 4.0-使用Jquery,如何调用代码隐藏函数并将数据返回给客户端? - ASP.NET 4.0 - Using Jquery, how do I call a codebehind function and return data to the client? 如何从 ASP.NET 核心中的 JSON 请求正文中读取多个参数? - How do I read multiple parameters from a JSON request body in ASP.NET Core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM