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();
    }
}
fehays
  • 3,147
  • 1
  • 24
  • 43
  • Would just checking `ContentType` on your response to see if it's one of the types you'll be interested in processing the data from work? – mlorbetske Apr 19 '13 at 21:26
  • I was considering that, but wasn't sure how safe that is. Like if the web server could return a content-type of text in the http header and still return binary data. – fehays Apr 19 '13 at 21:27
  • that's entirely possible but I'd hope it to be a pretty rare case – mlorbetske Apr 19 '13 at 21:29
  • If you don't want to rely on `ContentType` tag, for my intuition you are not able to determine what is the content without actually reading it and then checking for some control characters to assume with pretty high probability, you have some binary data, as there is small chance to get many of non-printable characters in text content. – jwaliszko Apr 19 '13 at 21:32

1 Answers1

4

In general, web sites will tell you in the Content-Type header what kind of data they're returning. You can determine that by getting the ContentType property from the response.

But sites have been known to lie. Or not say anything. I've seen both. If there is no Content-Type header or you don't want to trust it, then the only way you can tell what kind of data is there, is by reading it.

But then, if you don't trust the site, why are you reading data from it?

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • I can't provide the details but I can assure you that I need to read the data, and I do not trust the site. I'll go the route of reading the data. I've found some info on checking mime type from the stream. – fehays Apr 20 '13 at 00:26
  • I would suggest then reading it as binary, and then examining the data. You might get away with reading only 1 kilobyte or so to start. That's usually enough to tell you what it is. – Jim Mischel Apr 20 '13 at 00:51
  • Thanks. I ended up using this solution to find the mime type from the first 256 bytes: http://stackoverflow.com/questions/58510/using-net-how-can-you-find-the-mime-type-of-a-file-based-on-the-file-signature. I'll accept your answer as the solution since you got me pointed in the right direction. If you don't mind, please add the above link to your answer. Thanks again – fehays Apr 22 '13 at 16:01