简体   繁体   中英

WebResponse Content-Length Returns -1

I have tried this code with other links and its works. But on this link content-length returns -1.

Methot 1 :

System.Net.WebRequest wr = System.Net.HttpWebRequest.Create(_link);
wr.Method = "HEAD";
using (System.Net.WebResponse resp = wr.GetResponse())
{
   string ContentLength = resp.Headers.Get("Content-Length");
}

Methot 2 :

var request = (HttpWebRequest)WebRequest.Create(_link);
request.Method = "HEAD";
using (var response = request.GetResponse())
{
    long ContentLength = response.ContentLength;
}

How can i get the Content-Length?

If you get -1, there is no Content-Length header in the response.

That make sense with the HEAD verb, which does not send any content, just the headers.

If you change the verb by GET, the content length will return the reponse length.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM