简体   繁体   English

ServiceStack JsonServiceClient 响应头

[英]ServiceStack JsonServiceClient Response Header

I'm new to ServiceStack so please forgive my ignorance...我是 ServiceStack 的新手,所以请原谅我的无知...

How can I the JsonServiceClient to give my my DataContract response object and also allow me to get the response header with only a single request?我怎样才能让JsonServiceClient提供我的DataContract响应对象并允许我仅通过一个请求获取响应标头?

I've figured out how to return a populated DataContract Response object like this:我已经想出了如何像这样返回一个填充的 DataContract Response 对象:

var field1 = "aaa";
var field2 = "bbb";

var client = new JsonServiceClient("https://api.server.com/api");
Response1 response1 = client.Post(new Request1() { Field1= field1, Field2= field2});

I've also figured out how to get a response header item from the HttpWebResponse overload like this (see below) which requires the slight modification of commenting out the request data contract interface as shown here: class Request1 //: IReturn<Response1> :我还想出了如何从这样的HttpWebResponse重载中获取响应标头项(见下文),这需要稍微修改注释掉请求数据协定接口,如下所示: class Request1 //: IReturn<Response1>

var field1 = "aaa";
var field2 = "bbb";

var client = new JsonServiceClient("https://api.server.com/api");
HttpWebResponse response1 = client.Post(new Request1() { Field1= field1, Field2= field2});
var value = response1.Headers.GetValues("Item")[0];

But again, how can I do both at once?但同样,我怎么能同时做到这两点呢?

I did figure out how to do this with the .PostJsonToUrl() extension method but my question is how can I do this with the JsonServiceClient .我确实想出了如何使用.PostJsonToUrl()扩展方法来做到这一点,但我的问题是如何使用JsonServiceClient做到这JsonServiceClient If anyone is curious here's how I did it with the .PostJsonToUrl() extension method:如果有人好奇这里是我如何使用.PostJsonToUrl()扩展方法.PostJsonToUrl()

var url = "https://api.server.com/api/method";
string value = null;
Response1 response1 = url.PostJsonToUrl(new Request1() { Field1= field1, Field2= field2},
    null, httpResponse =>
    {
        value = httpResponse.Headers.GetValues("Item")[0];
    }).FromJson<Request1>();

Here are my DataContract Classes这是我的 DataContract 类

[Route("/method")]
[DataContract]
class Request1 : IReturn<Response1>
{
    [DataMember(Name = "field1")]
    public string Field1 { get; set; }
    [DataMember(Name = "field2")]
    public string Field2 { get; set; }
}

[DataContract]
class Response1
{
    [DataMember(Name = "field1")]
    public long Field1 { get; set; }

    [DataMember(Name = "field2")]
    public string Field2 { get; set; }

    [DataMember(Name = "field3")]
    public string Field3 { get; set; }
}

All ServiceStack's .NET Clients include both a ResponseFilter and static GlobalResponseFilter property you can use to inspect the HTTP Response before the response is deserialized, eg:所有ServiceStack 的 .NET 客户端都包含ResponseFilter和静态GlobalResponseFilter属性,您可以使用它们在反序列化响应之前检查 HTTP 响应,例如:

string myHeader = null;
var client = new JsonServiceClient(baseUrl) {
    ResponseFilter = res => myHeader = res.Headers["X-MyHeader"],
};

var response = client.Post(request);

Where response will contain the deserialized Response body and myHeader will be populated with the value of X-MyHeader HTTP Header.其中response将包含反序列化的 Response 正文,而myHeader将填充X-MyHeader HTTP Header 的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在ServiceStack JsonServiceClient上设置Accept标头 - Set Accept header on ServiceStack JsonServiceClient ServiceStack JsonServiceClient 请求不一致 - ServiceStack JsonServiceClient Requests not consistent ServiceStack.JsonServiceClient.HttpLog 未填充 - ServiceStack.JsonServiceClient.HttpLog is not populating 反序列化时,ServiceStack JsonServiceClient静默失败 - ServiceStack JsonServiceClient Silently Failing When Deserializing ServiceStack JsonServiceClient,强制为localhost线路上的流量? - ServiceStack JsonServiceClient, force traffic on the wire for localhost? ServiceStack JsonServiceClient使用路由属性发送会引发未找到异常 - ServiceStack JsonServiceClient Send using route attributes throws exception not found 类型&#39;ServiceStack.JsonServiceClient&#39;中的方法&#39;Get&#39;…没有实现 - Method 'Get' in type 'ServiceStack.JsonServiceClient' … does not have an implementation 提取数据时,ServiceStack MsgPackServiceClient失败,但JsonServiceClient起作用 - ServiceStack MsgPackServiceClient fails when fetching data but JsonServiceClient works 什么是在Servicestack JsonServiceClient Get方法上实现重试的最佳解决方案? - Whats the best solution to implement retry on Servicestack JsonServiceClient Get method? ServiceStack:如何从JsonServiceClient的Get方法获取StatusCode - ServiceStack :How to get StatusCode from JsonServiceClient Get method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM