HttpContext 的请求正文 - 如何获取值
Request body of HttpContext - How grab values
这是 HttpContext
的请求正文:
string body = string.Empty;
context.Request.InputStream.Position = 0;
using (var inputStream = new StreamReader(context.Request.InputStream))
{
body = inputStream.ReadToEnd();
}
--------------------------c6fec46ab7e1fd1e Content-Disposition: form-data; name="id" 61dab40eedc5663a2436f726
--------------------------c6fec46ab7e1fd1e Content-Disposition: form-data; name="txid"
a28fc26550cea7a67a9725625a3e476c4df9118f066f6360b4fbaeb633a789f9
--------------------------c6fec46ab7e1fd1e Content-Disposition: form-data; name="explorer_url"
https://coinremitter.com/tcn/tx/a28fc26550cea7a67a9725625a3e476c4df9118f066f6360b4fbaeb633a789f9?from=coinremitter
--------------------------c6fec46ab7e1fd1e Content-Disposition: form-data; name="merchant_id" 61d97b1a7e7f76732e105a37
--------------------------c6fec46ab7e1fd1e Content-Disposition: form-data; name="type" send --------------------------c6fec46ab7e1fd1e
Content-Disposition: form-data; name="coin_short_name" TCN
--------------------------c6fec46ab7e1fd1e Content-Disposition: form-data; name="wallet_id" 61daa2ad7cc73878a6383a42
--------------------------c6fec46ab7e1fd1e Content-Disposition: form-data; name="wallet_name" TestWallet110
--------------------------c6fec46ab7e1fd1e Content-Disposition: form-data; name="address" RKKxYDkvWp8UUjUu2sYgdfPMMbfmqYDSa1
--------------------------c6fec46ab7e1fd1e Content-Disposition: form-data; name="amount" 0.50000000
--------------------------c6fec46ab7e1fd1e Content-Disposition: form-data; name="confirmations" 3
--------------------------c6fec46ab7e1fd1e Content-Disposition: form-data; name="date" 2022-01-09 10:08:14
--------------------------c6fec46ab7e1fd1e--
如您所见,这个字符串不是 json。
我如何从这个字符串中获取地址、金额、确认和日期?
在不使用请求流的情况下,我认为因为请求的内容类型是 form-data
你可以像这样在请求上使用 Form
函数:
var address = context.Request.Form["address"].ToString();
var amount = context.Request.Form["amount"].ToString();
//and so on for other data
这是 HttpContext
的请求正文:
string body = string.Empty;
context.Request.InputStream.Position = 0;
using (var inputStream = new StreamReader(context.Request.InputStream))
{
body = inputStream.ReadToEnd();
}
--------------------------c6fec46ab7e1fd1e Content-Disposition: form-data; name="id" 61dab40eedc5663a2436f726 --------------------------c6fec46ab7e1fd1e Content-Disposition: form-data; name="txid" a28fc26550cea7a67a9725625a3e476c4df9118f066f6360b4fbaeb633a789f9 --------------------------c6fec46ab7e1fd1e Content-Disposition: form-data; name="explorer_url" https://coinremitter.com/tcn/tx/a28fc26550cea7a67a9725625a3e476c4df9118f066f6360b4fbaeb633a789f9?from=coinremitter --------------------------c6fec46ab7e1fd1e Content-Disposition: form-data; name="merchant_id" 61d97b1a7e7f76732e105a37 --------------------------c6fec46ab7e1fd1e Content-Disposition: form-data; name="type" send --------------------------c6fec46ab7e1fd1e Content-Disposition: form-data; name="coin_short_name" TCN --------------------------c6fec46ab7e1fd1e Content-Disposition: form-data; name="wallet_id" 61daa2ad7cc73878a6383a42 --------------------------c6fec46ab7e1fd1e Content-Disposition: form-data; name="wallet_name" TestWallet110 --------------------------c6fec46ab7e1fd1e Content-Disposition: form-data; name="address" RKKxYDkvWp8UUjUu2sYgdfPMMbfmqYDSa1 --------------------------c6fec46ab7e1fd1e Content-Disposition: form-data; name="amount" 0.50000000 --------------------------c6fec46ab7e1fd1e Content-Disposition: form-data; name="confirmations" 3 --------------------------c6fec46ab7e1fd1e Content-Disposition: form-data; name="date" 2022-01-09 10:08:14 --------------------------c6fec46ab7e1fd1e--
如您所见,这个字符串不是 json。
我如何从这个字符串中获取地址、金额、确认和日期?
在不使用请求流的情况下,我认为因为请求的内容类型是 form-data
你可以像这样在请求上使用 Form
函数:
var address = context.Request.Form["address"].ToString();
var amount = context.Request.Form["amount"].ToString();
//and so on for other data