[英]How to get response type of the request without loading the full content
如何在不加载完整内容的情况下获取请求的响应类型?我只对获取响应的ContentType感兴趣。
下面是我正在做的代码。
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;
}
简单。 发出HEAD请求。 这指示服务器从响应中省略响应主体。
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "HEAD";
using (var response = (HttpWebResponse)request.GetResponse())
{
//...
您可以发送HTTP HEAD请求,该请求应该为您提供标题但不包含正文。
请注意,并非所有服务器都会回答HEAD请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.