从RestSharp响应标头获取价值 [英] Get value from RestSharp response headers

查看:263
本文介绍了从RestSharp响应标头获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人可以帮助我解决RestSharp遇到的问题.一切正常;我正在使用以下代码获得答复:

I'm hoping someone can help with an issue I'm having with RestSharp. It's all working; I'm getting my reply using the following code:

var client = new RestClient("http://mybaseuri.com");
var request = new RestRequest("service/{id}", Method.GET);
request.AddUrlSegment("id", id);
    
// execute the request
IRestResponse response = client.Execute(request);

我想从响应标头中获取以下值:

I'd like to get a value from the response headers along the lines of:

string userId = response.Headers["userId"]

我在网上找不到任何示例,有人可以帮助从类型为IList<parameter>response.Headers对象获取值吗?

I can't find any examples online, can anyone help get a value from the response.Headers object which is of type IList<parameter>?

我不想遍历整个集合,只是按名称提取单个值.

I'm not looking to iterate the entire collection, just extract a single value by name.

推荐答案

当我想知道自己该怎么做时,我借助

As I was wondering myself how to do the same, I find out the following solution with the help of this MSDN article about IList<T>:

string userId = response.Headers.ToList()
    .Find(x => x.Name == "userId")
    .Value.ToString();

我必须承认我很喜欢使用 即时窗口 快速观看 在Visual Studio中玩深入了解我正在处理的是哪种元素类型:

I need to confess I'm fond of playing in Visual Studio with Immediate Window or Quick Watch to dig a bit and know what kind of element Type I'm dealing with:

response.Headers[0]

{userId = 1024}

{userId=1024}

名称:"userId"

Name: "userId"

类型:HttpHeader

值:"1024"

这篇关于从RestSharp响应标头获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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