HttpClient检索所有标头

编程入门 行业动态 更新时间:2024-10-20 03:20:25
本文介绍了HttpClient检索所有标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

目前,我正在研究API包装器。如果我发送错误的消费者密钥,服务器将返回状态为 403 Forbidden 。它还将传递自定义标头。如何实际检索这些自定义标头?

Currently, I am working on API wrapper. If I send a bad Consumer Key, the server will return Status as 403 Forbidden in the header. It will also pass custom headers. How do I actually retrieve these custom headers?

这是从服务器收到的响应。

This is the response receive from the server.

Cache-Control: private Date: Wed, 01 May 2013 14:36:17 GMT P3P: policyref="/w3c/p3p.xml", CP="ALL CURa ADMa DEVa OUR IND UNI COM NAV INT STA PRE" Server: Apache/2.2.23 (Amazon) Status: 403 Forbidden X-Error: Invalid consumer key. X-Error-Code: 152 X-Powered-By: PHP/5.3.20 Connection: keep-alive

我需要检索 X-Error 和 X-Error-Code 。目前,我正在使用 HttpClient 类来处理请求。如果我在VS Studio 2012的Quick Watch下看到标题响应,我可以像这样找到它

I need to retrieve the X-Error and X-Error-Code. Currently, I am using HttpClient class to process the request. If I watch the headers respond under Quick Watch in VS Studio 2012, I could find it like this

((System.Net.Http.Headers .HttpHeaders)(response.Headers))。headerStore [X-Error-Code]。ParsedValue

有没有其他方法可以这样做?

Is there any other way to do this?

编辑: headerStore 无法通过代码访问,因为这是私有字段。我只能通过快速观察窗口访问它。

headerStore is not accessible thru code as this is private field. I only get access to it through the Quick Watch window.

这是我的请求代码段:

var response = await _httpClient.PostAsync("/v3/oauth/request", content);

推荐答案

好吧, HttpResponseMessage.Headers 返回 HttpResponseHeaders 参考,所以你应该可以使用 GetValues()

Well, HttpResponseMessage.Headers returns an HttpResponseHeaders reference, so you should be able to use GetValues()

string error = response.Headers.GetValues("X-Error").FirstOrDefault(); string errorCode = response.Headers.GetValues("X-Error-Code").FirstOrDefault();

更多推荐

HttpClient检索所有标头

本文发布于:2023-11-16 16:30:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1605956.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:HttpClient

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!