簡體   English   中英

如何在不加載完整內容的情況下獲取請求的響應類型

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM