简体   繁体   English

C# HTTP 请求中的偶尔响应头

[英]Occasional response headers in C# HTTP Request

Request:要求:

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
String responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
Console.WriteLine(responseString);

Response:回复:

{"code":"SUCCESS","details": 
    {"created_time":"","id":"xxxx"},  
    "message":"uploaded", 
    "status":"success"}
HTTP/1.1 200 OK 
Date: Wed, 18 Dec 2019 11:42:26 IST
Last-Modified: Wed, 18 Dec 2019 11:42:25 IST
Content-Type: application/json
Connection: Keep-Alive
Server: AWServer
Pragma: no-cache
Cache-Control: no-cache
Expires: 1

Whenever the above-mentioned C# request is executed, the response occasionally contains headers(HTTP/1.1 200 OK...), When I'm only trying to get the body part({"code"....} alone(response.GetResponseStream()). Is this the intended behavior?每当执行上述 C# 请求时,响应偶尔会包含标头(HTTP/1.1 200 OK...),当我只尝试获取正文部分({“代码”....} 单独(响应.GetResponseStream()). 这是预期的行为吗?

Take a look at the basic article on http headers看一下http headers的基础文章

HTTP headers let the client and the server pass additional information with an HTTP request or response. HTTP 标头让客户端和服务器通过 HTTP 请求或响应传递附加信息。 An HTTP header consists of its case-insensitive name followed by a colon (:), then by its value. HTTP 标头由不区分大小写的名称、后跟冒号 (:) 和其值组成。 Whitespace before the value is ignored.忽略值前的空格。

Headers are additional information.标题是附加信息。 I guess that since you left out the url and the whole creation of the Request and the url, this means that some responses have Headers and some not.我想,由于您遗漏了 url 以及请求和 url 的整个创建,这意味着有些响应有Headers ,有些没有。 That depends on the additional non-body information the api or web server wants to respond with.这取决于 api 或 Web 服务器想要响应的其他非主体信息。

It's in the control of the responder and not the receiver.它由响应者而不是接收者控制。

Don't ignore them : Some times interesting metadata come from Headers.不要忽略它们:有时有趣的元数据来自标题。 It should not be data but information about it, like encoding, CORS info etc.它不应该是data而是有关它的信息,例如编码、CORS 信息等。

last modified header link date header link 最后修改标题链接 日期标题链接

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM