在ServiceStack中查看Http Response内容 [英] Seeing the Http Response content in ServiceStack

查看:115
本文介绍了在ServiceStack中查看Http Response内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ServiceStack为JSON RESTful服务创建C#客户端.我有这段代码返回了我的DTO:

I am using ServiceStack to create a C# client to a JSON RESTful service. I have this code that returns my DTO:

搜索结果= restClient.Get(搜索);

Search result = restClient.Get (search);

这很好,但是为了有效地调试返回的搜索结果,我需要从基础的HTTP Response对象输出文本内容. (我尚不知道响应中的所有元素是否已将其添加到DTO中.)

This works fine, but in order to effectively debug the search results coming back I need to output the text content from the underlying HTTP Response object. (I don't know all the elements in the response yet in order to add them to the DTO).

有什么方法可以从结果对象中获取基本的HTTP响应,以及全文内容吗?

Is there any way I can get hold of the underlying HTTP response, and thus the full text content, from my result object?

先谢谢了. @adamfowleruk

Thanks in advance. @adamfowleruk

推荐答案

从ServiceStack内置的 Service 继承时,您可以使用以下方法直接从Response类访问基础的Request和Response:

When inheriting from ServiceStack's built-in Service you can access the underlying Request and Response directly from the Response class with:

public class MyService : Service 
{
    public object Get(Request request)
    {
        base.Request ...
        base.Response ...
    }
}

您不会在服务或过滤器中看到响应输出,因为它直接写入响应流并且是在执行您的服务和所有响应筛选器之后,ServiceStack要做的最后一件事.

You won't see the response output in your service or filters since it writes directly to the response stream and is the last thing that ServiceStack does after executing your service and all response filters.

对于诊断HTTP,我建议也使用Fiddler或WebInspector ServiceStack的内置请求记录器也可能有帮助.

For diagnosing HTTP I recommend using Fiddler or WebInspector also ServiceStack's built-in Request Logger might help as well.

如果您使用的是 C#服务客户端,则只需问一下为了你想要的,例如您可以将返回的响应作为原始字符串访问:

If you're using the C# Service Clients you can simply ask for what you want, e.g. you can access the returned response as a raw string:

string responseJson = client.Get<string>("/poco/World");

或作为原始字节:

byte[] responseBytes = client.Get<byte[]>("/poco/World");

或作为流:

using (Stream responseStream = client.Get<Stream>("/poco/World")) {
    var dto = responseStream.ReadFully().FromUtf8Bytes().FromJson<PocoResponse>();
}

或者甚至访问填充的HttpWebResponse对象:

Or even access the populated HttpWebResponse object:

HttpWebResponse webResponse = client.Get<HttpWebResponse>("/poco/World");

webResponse.Headers["X-Response"] //World
using (webResponse)
using (var stream = webResponse.GetResponseStream())
using (var sr = new StreamReader(stream)) {
    string response = sr.ReadToEnd();
}

您还可以使用全局和本地响应过滤器对HttpWebResponse进行内部检查,例如:

You can also introspect the HttpWebResponse by using Global and Local Response filters, e.g:

JsonServiceClient.HttpWebResponseFilter = httpRes => { .. };

或使用本地过滤器:

var client = new JsonServiceClient(baseUrl) { 
    ResponseFilter = httpRes => { .. }
};

使用第三者服务

如果您使用的是第三方REST/HTTP API,则可以在

这篇关于在ServiceStack中查看Http Response内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆