简体   繁体   中英

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.

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. 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.

Note that not all servers will answer HEAD requests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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