I want to get type of a web address. For example this is a Html page and its page type is text/html
but the type of this is text/xml
. this page's type seems to be image/png
but it's text/html
.
I want to know how can I detect the content type of a web address like this?
using (MyClient client = new MyClient())
{
client.HeadOnly = true;
string uri = "http://www.google.com";
byte[] body = client.DownloadData(uri); // note should be 0-length
string type = client.ResponseHeaders["content-type"];
client.HeadOnly = false;
// check 'tis not binary... we'll use text/, but could
// check for text/html
if (type.StartsWith(@"text/"))
{
string text = client.DownloadString(uri);
Console.WriteLine(text);
}
}
Will get you the mime type from the headers without downloading the page. Just look for the content-type in the response headers.
it should be something like this
var request = HttpWebRequest.Create("http://www.google.com") as HttpWebRequest;
if (request != null)
{
var response = request.GetResponse() as HttpWebResponse;
string contentType = "";
if (response != null)
contentType = response.ContentType;
}
HTTP Response header: content-type
For a more detailed response, please provide a more detailed question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With