[英]How to get response type of the request without loading the full content
How to get the response type of the request without loading the full content?, I'm only interested in getting ContentType of the response. 如何在不加载完整内容的情况下获取请求的响应类型?我只对获取响应的ContentType感兴趣。
below is the code what I'm doing. 下面是我正在做的代码。
public static bool OutPutFormat(string url, string type)
{
var request = (HttpWebRequest)WebRequest.Create(url);
using (var response = (HttpWebResponse)request.GetResponse())
{
string _type = "application/" + type;
string _apiType = response.ContentType.Split(';')[0].ToString();
if (_apiType == _type)
{
return true;
}
}
return false;
}
Easy. 简单。 Issue a HEAD request.
发出HEAD请求。 This instructs the server to omit the response body from the response.
这指示服务器从响应中省略响应主体。
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "HEAD";
using (var response = (HttpWebResponse)request.GetResponse())
{
//...
You can send an HTTP HEAD request, which should give you headers but no body. 您可以发送HTTP HEAD请求,该请求应该为您提供标题但不包含正文。
Note that not all servers will answer HEAD requests. 请注意,并非所有服务器都会回答HEAD请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.