简体   繁体   English

如何在不加载完整内容的情况下获取请求的响应类型

[英]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.

相关问题 C#获取响应的内容类型而不加载整个内容 - C# Get content type of a response without loading entire content 在不加载其程序集的情况下获取引用类型的全名 - Get the full name of a referenced type without loading its assembly (UWP)如何在GET请求中添加不带Content-Length的Content-Type标头 - (UWP) How to add Content-Type header without Content-Length in GET request 获取内容WCF响应的类型 - Get content Type of WCF response 如何在我对Get请求的响应中使用c#创建multipart / form-data类型的内容 - How do I create the content of type multipart/form-data using c# as part of my response to Get request Puppeteer-sharp 加载页面时如何获取所有网络请求和完整响应数据? - How can I get all network requests and full response data when loading a page by Puppeteer-sharp? 如何将Content-Type标头添加到.Net GET Web请求? - How to add the Content-Type header to a .Net GET web request? 如何接受 JSON 作为其内容类型的无主体 GET 请求? - How to accept a bodyless GET request with JSON as it's content type? 如何在不下载内容的情况下执行GET请求? - How can I perform a GET request without downloading the content? 获取没有完整命名空间的类型名称 - Get type name without full namespace
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM