How to Use the Http Head method to obtain the object Length

Source: Internet
Author: User

Requirement
There is a fixed URL file which will be updated regularly by the server program. Now you need to write a tool to monitor the file changes.
Solution
At first, I thought of downloading the file, and then determining whether the file is changed by size (it is known that the change of the file will change greatly ).
However, this file is sometimes very large. If it is downloaded every time, it will take a certain amount of time and hopefully it will be faster.
After searching, we found that in addition to the Get and Post methods, Http also has the Head method, which can Get the http header information, where Content-Length is the file size.
Theory
When the Method attribute is set to Head in HttpWebRequest, only the http header information can be obtained without returning the actual content.
In addition to Get, Post, and Head, you can also set the Method attribute: Copy codeThe Code is as follows: The Method property is set to any HTTP 1.1 protocol predicate: GET, HEAD, POST, PUT, DELETE, TRACE, or OPTIONS.

In the Http protocol, the response obtained by the Head method is the same as that obtained by the Get method except that there is no body content. That is to say:
Get: http header information + content
Head: http header information
In this way, if we only care about the http header and do not need the content, we can use the Head method.
Practice
Copy codeThe Code is as follows: static void Main (string [] args)
{
Var url = "http://www.google.com/intl/en_ALL/images/srpr/logo1w.png ";
Var len = GetHttpLength (url );
Console. WriteLine ("Url: {0} \ r \ nLength: {1}", url, len );
}
Static long GetHttpLength (string url)
{
Var length = 0l;
Try
{
Var req = (HttpWebRequest) WebRequest. CreateDefault (new Uri (url ));
Req. Method = "HEAD ";
Req. Time out = 5000;
Var res = (HttpWebResponse) req. GetResponse ();
If (res. StatusCode = HttpStatusCode. OK)
{
Length = res. ContentLength;
}
Res. Close ();
Return length;
}
Catch (WebException wex)
{
Return 0;
}
}

The output is as follows:
Url: http://www.google.com/intl/en_ALL/images/srpr/logo1w.png
Length: 6803
Note:The Head method is the same as the Get method. Sometimes, if the server sets the cache, the same content will be returned. At this time, you can add a time parameter after the url to invalidate the cache for real-time retrieval.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.