如何使用 RESTSharp 获取响应标头?

ALD*_*FE 1 c# api restsharp

我正在开发一个调用 API 的程序,一切正常,但我必须恢复响应头上的一些信息,我该如何恢复这些信息?

我尝试过类似的东西:string h = response.Headers;但它不起作用。

 var client = new RestClient("https://xxxx.com/");

        client.Authenticator = new HttpBasicAuthenticator("user", "password");


        var request = new RestRequest("xx/xx/xx", Method.GET);
        IRestResponse response = client.Execute(request);
        var xml_text = response.Content;
Run Code Online (Sandbox Code Playgroud)

db2*_*db2 5

我很确定 RestSharp 中的响应标头作为集合 ( IList)返回,因此声明h为字符串将不起作用。请参阅此处的来源。您可能想尝试将值转换为这样的字符串:

foreach (var h in response.Headers)
{
  h.ToString();
}
Run Code Online (Sandbox Code Playgroud)

如果你知道你要找的头的名称,你可以使用有点像显示的那样LINQ的在这里

string userId = response.Headers
    .Where(x => x.Name == "userId")
    .Select(x => x.Value)
    .FirstOrDefault().ToString();
Run Code Online (Sandbox Code Playgroud)