Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to request only the HTTP header with C#?

I want to check if the URL of a large file exists. I'm using the code below but it is too slow:

public static bool TryGet(string url) {     try     {         GetHttpResponseHeaders(url);         return true;     }     catch (WebException)     {     }      return false; }  public static Dictionary<string, string> GetHttpResponseHeaders(string url) {     Dictionary<string, string> headers = new Dictionary<string, string>();     WebRequest webRequest = HttpWebRequest.Create(url);     using (WebResponse webResponse = webRequest.GetResponse())     {         foreach (string header in webResponse.Headers)         {             headers.Add(header, webResponse.Headers[header]);         }     }      return headers; } 
like image 618
Jader Dias Avatar asked Jun 04 '11 15:06

Jader Dias


People also ask

Can an HTTP request have custom headers?

Another example of using a custom HTTP header would be to implement the X-Pull header. You can use this custom header for a variety of purposes including rate limiting bandwidth on your origin server, restricting CDN traffic, creating custom logic on your origin server, etc.

How do I request a header size?

go to the network tab and right click the first item and click copy as cURL (this is how you will get the header size. Then go to terminal and do your curl command curl ... -w '%{size_request} %{size_upload}' which will print out the request size at the end.

What is request header syntax structure?

It is a request type header. The Accept header is used to inform the server by the client that which content type is understandable by the client expressed as MIME-types. Accept-charset. It is a request type header. This header is used to indicate what character set are acceptable for the response from the server.


1 Answers

You need to set:

webRequest.Method = "HEAD"; 

This way the server will respond with the header information only (no content). This is also useful to check if the server accepts certain operations (i.e. compressed data etc.).

like image 125
Teoman Soygul Avatar answered Sep 23 '22 02:09

Teoman Soygul