我有一个继承自ApiController的类.它有一个像这样的Put方法:
[PUT("user/{UserId}")]
public HttpResponseMessage Put(string userId, PaymentRequest paymentRequest)
{
// Calling business logic and so forth here
// Return proper HttpResponseMessage here
}
Run Code Online (Sandbox Code Playgroud)
该方法在上面工作正常.现在我需要验证方法调用的签名,但在这里我遇到了一个问题.签名本质上是方法+ url + body的组合.我可以通过调用Request.Method和我可以通过调用Request.RequestUri.ToString()得到的url获得的方法,但是我无法得到它之前的身体,因为它被自动反序列化为PaymentRequest对象之前 asp.net MVC4框架.
我的第一次尝试: 因为我现在已经理解了Request.Content.ReadAsStringAsync().结果什么也没有返回.这是因为内容只能读取一次.
我的第二次尝试: 我尝试将其序列化为JSON字符串.
var serializer = new JavaScriptSerializer();
var paymentRequestAsJson = serializer.Serialize(paymentRequest);
Run Code Online (Sandbox Code Playgroud)
这个问题是格式化与签名的正文部分略有不同.它具有相同的数据,但有一些空格.
我无法改变Put-method的调用者所做的事情,因为这是第三方组件.我该怎么办?
我正在尝试从新的Asp.Net Web Api中的请求中提取一些数据.我有这样的处理程序设置:
public class MyTestHandler : DelegatingHandler
{
protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
if (request.Content.IsFormData())
{
request.Content.ReadAsStreamAsync().ContinueWith(x => {
var result = "";
using (var sr = new StreamReader(x.Result))
{
result = sr.ReadToEnd();
}
Console.Write(result);
});
}
return base.SendAsync(request, cancellationToken);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的http请求:
POST http://127.0.0.1/test HTTP/1.1
Connection: Keep-Alive
Content-Length: 29
Content-Type: application/x-www-form-urlencoded
Expect: 100-continue
Host: 127.0.0.1
my_property=my_value
Run Code Online (Sandbox Code Playgroud)
问题是无论我如何尝试从中读取信息request.Content总是空的.我试过了
request.Content.ReadAsStreamAsync
request.Content.ReadAsFormDataAsync
request.Content.ReadAs<FormDataCollection>
Run Code Online (Sandbox Code Playgroud)
以及
[HttpGet,HttpPost]
public string Index([FromBody]string my_property)
{
//my_property == null
return "Test";
} …Run Code Online (Sandbox Code Playgroud)