Getting the mime / content type of an http document
-
Hi, Is there any way I can get the actual content type of a URL without downloading the file completely ? 1. I tried CHttpFile::QueryInfo with HTTP_QUERY_CONTENT_TYPE, But for that to work, I must call SendRequest, which will download the complete file. 2. I tried FindMimeFromData(...) with Url option, But its not even connecting to the internet to get the Mime type, instead I believe it checks only in the url. Sometimes the url may not be having any information about this and the server might set the mime type, so I need a method which will download only the Http response header and extract this information. Thanks Jugs "A robust program is resistant to errors -- it either works correctly, or it does not work at all; whereas a fault tolerant program must actually recover from errors."
-
Hi, Is there any way I can get the actual content type of a URL without downloading the file completely ? 1. I tried CHttpFile::QueryInfo with HTTP_QUERY_CONTENT_TYPE, But for that to work, I must call SendRequest, which will download the complete file. 2. I tried FindMimeFromData(...) with Url option, But its not even connecting to the internet to get the Mime type, instead I believe it checks only in the url. Sometimes the url may not be having any information about this and the server might set the mime type, so I need a method which will download only the Http response header and extract this information. Thanks Jugs "A robust program is resistant to errors -- it either works correctly, or it does not work at all; whereas a fault tolerant program must actually recover from errors."
You could ask the server - The response of a HTTP request gives the MIME type. To see what I'm talking about try this: 1. Open a command prompt and enter "telnet www.thecodeproject.com 80". 2. Now type the following, exactly (you will not be able to see what you're typing - The blank line at the end is important):
HEAD /images/standard/logotop.gif HTTP/1.1 Host: www.thecodeproject.com
The response from the server will indicate the content type (see the "Content-Type" header field). I'm not saying you have to do all this manually at the socket level (although to do so would not be hard), but if you can figure out how to send a HTTP "HEAD" request to the server and read the response using whatever framework or API you're into you should be able to do it. Steve
-
You could ask the server - The response of a HTTP request gives the MIME type. To see what I'm talking about try this: 1. Open a command prompt and enter "telnet www.thecodeproject.com 80". 2. Now type the following, exactly (you will not be able to see what you're typing - The blank line at the end is important):
HEAD /images/standard/logotop.gif HTTP/1.1 Host: www.thecodeproject.com
The response from the server will indicate the content type (see the "Content-Type" header field). I'm not saying you have to do all this manually at the socket level (although to do so would not be hard), but if you can figure out how to send a HTTP "HEAD" request to the server and read the response using whatever framework or API you're into you should be able to do it. Steve
Hi Steve, That was indeed very helpful. I used plain sockets and got this to work. Thanks a TON!!! Jugs "A robust program is resistant to errors -- it either works correctly, or it does not work at all; whereas a fault tolerant program must actually recover from errors."