C # Get HTTP header, just header, not contain body [C # delicious] use the HTTP head method to get the file length

Source: Internet
Author: User
Tags intl
ArticleDirectory
    • Requirement
    • Solution
    • Theory
    • Practice
    • Requirement
    • Solution
    • Theory
    • Practice
[C # delicious] file length needs to be obtained using the HTTP Head Method

There is a fixed URL file on the server sideProgramThis file will be updated regularly. Now you need to write a tool to monitor changes to this file.

 

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:

The method attribute 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
 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 . Timeout = 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.

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:

The method attribute 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
 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 . Timeout = 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 :< strong style = "margin: 0px; padding: 0px; "> the head method is the same as the get method. Sometimes, if the server sets the cache, the same content will be returned. In this case, you can add a time parameter after the URL to invalidate the cache for real-time retrieval.

Related Article

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.