RestSharp打印RAW请求和响应头 [英] RestSharp print raw request and response headers
问题描述
我使用 RestSharp
做出一个Web服务调用。一切都很好,但我不知道是否有可能以打印原始请求头和身体被发送出去,原始响应头和回来的响应体。
I'm using RestSharp
to make calls to a webservice. All is well but I was wondering if it would be possible to print the raw request headers and body that is sent out and the raw response headers and the response body that comes back.
这是我的代码,我创建一个请求,并获得回应
This is my code where I create a request and get a response back
public static TResponse ExecutePostCall<TResponse, TRequest>(String url, TRequest requestData, string token= "") where TResponse : new()
{
RestRequest request = new RestRequest(url, Method.POST);
if (!string.IsNullOrWhiteSpace(token))
{
request.AddHeader("TOKEN", token);
}
request.RequestFormat = DataFormat.Json;
request.AddBody(requestData);
// print raw request here
var response = _restClient.Execute<TResponse>(request);
// print raw response here
return response.Data;
}
这样,难道是可以打印的原始请求和响应?
so, Would it be possible to print the raw request and response?
推荐答案
.NET提供了自己的但功能强大的记录功能。这可以通过配置文件被打开。
.net provides its own yet powerful logging feature. This can be turned on via config file.
我发现这个提示的此处。约翰·希恩指出如何配置网络跟踪文章。 (记:我编辑提供的配置,关闭不必要的(对我来说)低级别的日志记录)
I found this tip here. John Sheehan pointed to How to: Configure Network Tracing article. (a note: I edited the config provided, turned off unnecessary (for me) low level logging).
<system.diagnostics>
<sources>
<source name="System.Net" tracemode="protocolonly" maxdatasize="1024">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
<source name="System.Net.Cache">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
<source name="System.Net.Http">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
</sources>
<switches>
<add name="System.Net" value="Verbose"/>
<add name="System.Net.Cache" value="Verbose"/>
<add name="System.Net.Http" value="Verbose"/>
<add name="System.Net.Sockets" value="Verbose"/>
<add name="System.Net.WebSockets" value="Verbose"/>
</switches>
<sharedListeners>
<add name="System.Net"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="network.log"
/>
</sharedListeners>
<trace autoflush="true"/>
</system.diagnostics>
这篇关于RestSharp打印RAW请求和响应头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!