C# 如何使用 RestSharp 访问 HTTP 请求正文?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16768314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access the HTTP request body using RestSharp?
提问by Epoc
I'm building a RESTful API client in C# .NET 3.5.
我正在用 C# .NET 3.5 构建一个 RESTful API 客户端。
I first started building it with the good old HttpWebClient(and HttpWebResponse), I could do whatever I wanted with. I were happy. The only thing I was stuck on was the automatic deserialization from JSON response.
我首先开始用旧的HttpWebClient(和HttpWebResponse)来建造它,我可以做任何我想做的事。我很高兴。我唯一遇到的问题是 JSON 响应的自动反序列化。
So, I've heard about a wonderful library called RestSharp (104.1) which eases the development of RESTful API clients, and automatically deserialize JSON and XML responses. I switched all my code on it, but now I realize I can't do things I could do with HttpWebClientand HttpWebResponse, like access and edit the raw request body.
所以,我听说了一个叫做 RestSharp (104.1) 的很棒的库,它简化了 RESTful API 客户端的开发,并自动反序列化 JSON 和 XML 响应。我在上面切换了我所有的代码,但现在我意识到我不能做我可以用HttpWebClientand做的事情HttpWebResponse,比如访问和编辑原始请求正文。
Anyone has a solution ?
任何人都有解决方案?
Edit : I know how to set the request body (with request.AddBody()), my problem is that I want to get this request body string , edit it, and re-set it in the request (updating the request body on the fly)
编辑:我知道如何设置请求正文(使用request.AddBody()),我的问题是我想获取此请求正文字符串,对其进行编辑,然后在请求中重新设置它(动态更新请求正文)
采纳答案by davmos
The request body is a type of parameter. To add one, you can do one of these...
请求正文是一种参数。要添加一个,您可以执行以下操作之一...
req.AddBody(body);
req.AddBody(body, xmlNamespace);
req.AddParameter("text/xml", body, ParameterType.RequestBody);
req.AddParameter("application/json", body, ParameterType.RequestBody);
To retrieve the body parameter you can look for items in the req.Parameterscollection where the Typeis equal to ParameterType.RequestBody.
要检索 body 参数,您可以在req.Parameters集合中查找Type等于 的项目ParameterType.RequestBody。
See code for the RestRequestclass here.
在此处查看RestRequest该类的代码。
Here is what the RestSharp docs on ParameterType.RequestBodyhas to say:
以下是RestSharp 文档的内容ParameterType.RequestBody:
If this parameter is set, it's value will be sent as the body of the request. The name of the Parameter is ignored, and so are additional RequestBody Parameters – only 1 is accepted.
RequestBody only works on POST or PUT Requests, as only they actually send a body.
If you have GetOrPost parameters as well, they will overwrite the RequestBody – RestSharp will not combine them but it will instead throw the RequestBody parameter away.
如果设置了这个参数,它的值将作为请求的正文发送。参数的名称被忽略,额外的 RequestBody 参数也被忽略——只接受 1。
RequestBody 仅适用于 POST 或 PUT 请求,因为只有它们实际发送正文。
如果您也有 GetOrPost 参数,它们将覆盖 RequestBody——RestSharp 不会组合它们,而是将 RequestBody 参数扔掉。
For reading/updating the body parameter on-the-fly, you can try:
要即时读取/更新 body 参数,您可以尝试:
var body = req.Parameters.FirstOrDefault(p => p.Type == ParameterType.RequestBody);
if (body != null)
{
Console.WriteLine("CurrentBody={0}", body.Value);
body.Value = "NewBodyValue";
}
Or failing that, create a new copy of the RestRequestobject with a different body.
或者失败了,创建一个RestRequest具有不同主体的对象的新副本。

