2

Is there a way to determine if the response from an HttpWebRequest in C# contains binary data vs. text? Or is there another class or function I should be using to do this?

Here's some sample code. I'd like to know before reading the StreamReader if the content is not text.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.someurl.com");
request.Method = WebRequestMethods.Http.Get;
using (WebResponse response = request.GetResponse())
{
    // check somewhere in here if the response is binary data and ignore it 
    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
    {
        string responseDetails = reader.ReadToEnd().Trim();
    }
}
4

1 回答 1

3

通常,网站会在 Content-Type 标头中告诉您它们返回的数据类型。您可以通过ContentType从响应中获取属性来确定这一点。

但众所周知,网站会撒谎。或者什么都不说。两个我都见过。如果没有 Content-Type 标头或者您不想信任它,那么您可以知道那里有哪些类型的数据的唯一方法就是阅读它。

但是,如果您不信任该站点,为什么要从中读取数据?

于 2013-04-20T00:07:12.367 回答