【问题标题】:How to get response type of the request without loading the full content如何在不加载完整内容的情况下获取请求的响应类型
【发布时间】:2013-11-01 17:12:12
【问题描述】:

如何在不加载完整内容的情况下获取请求的响应类型?、 我只对获取响应的 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;
    }

【问题讨论】:

    标签: c# content-type


    【解决方案1】:

    简单。发出 HEAD 请求。这指示服务器从响应中省略响应正文。

        var request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "HEAD";
        using (var response = (HttpWebResponse)request.GetResponse())
        {
             //...
    

    【讨论】:

      【解决方案2】:

      您可以发送一个 HTTP HEAD 请求,它应该给您标头但没有正文。

      请注意,并非所有服务器都会响应 HEAD 请求。

      【讨论】:

      • 1+ 谢谢,我不知道并非所有服务器都会响应 HEAD 请求调用
      猜你喜欢
      • 1970-01-01
      • 2011-02-14
      • 1970-01-01
      • 1970-01-01
      • 2021-01-08
      • 1970-01-01
      • 1970-01-01
      • 2012-10-06
      • 2012-07-12
      相关资源
      最近更新 更多