locked
HttpWebRequest - Content Length is always -1? RRS feed

  • Question

  • When I try this code, Content Length is always -1.

     

     Dim Request As HttpWebRequest = DirectCast(WebRequest.Create("http://www.google.com"), HttpWebRequest)
     Dim Response As HttpWebResponse = Request.GetResponse()
     Dim Length As Long = response.ContentLength
     MsgBox(Length)
    
    Friday, June 10, 2011 5:26 AM

Answers

  • If you have looked into the help for this property, you would have noticed the statement: "The ContentLength property contains the value of the Content-Length header returned with the response. If the Content-Length header is not set in the response, ContentLength is set to the value -1."

    http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.contentlength.aspx

    Simply put, http://www.google.com does not have the header field Content-Length in its header. Replace that URL with this thread's url and you will get some value other than -1


    Please mark this as answer or vote as helpful if it solved your problem
    • Proposed as answer by Mike Feng Monday, June 13, 2011 7:36 PM
    • Marked as answer by Mike Feng Tuesday, June 21, 2011 1:11 PM
    Friday, June 10, 2011 1:16 PM

All replies

  • When I try this code, Content Length is always -1.

     

     Dim Request As HttpWebRequest = DirectCast(WebRequest.Create("http://www.google.com"), HttpWebRequest)
     Dim Response As HttpWebResponse = Request.GetResponse()
     Dim Length As Long = response.ContentLength
     MsgBox(Length)
    

    Do you have a question?  The code is explained well in the help files.  Start at "HttpWebRequest".
    Friday, June 10, 2011 5:44 AM
  • I am trying to find out why my Content Length is always -1.
    Friday, June 10, 2011 11:42 AM
  • I am trying to find out why my Content Length is always -1.

    Did you look in the help files?
    Friday, June 10, 2011 11:51 AM
  • Take a look at the first 10 rows (code lines) in this sample on our website.

    http://www.vb-tips.com/MSHTML.aspx


    Success
    Cor
    Friday, June 10, 2011 12:02 PM
  • If you've actually read this thread you would've seen that I use code similar to that one.
    Friday, June 10, 2011 12:46 PM
  • If you have looked into the help for this property, you would have noticed the statement: "The ContentLength property contains the value of the Content-Length header returned with the response. If the Content-Length header is not set in the response, ContentLength is set to the value -1."

    http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.contentlength.aspx

    Simply put, http://www.google.com does not have the header field Content-Length in its header. Replace that URL with this thread's url and you will get some value other than -1


    Please mark this as answer or vote as helpful if it solved your problem
    • Proposed as answer by Mike Feng Monday, June 13, 2011 7:36 PM
    • Marked as answer by Mike Feng Tuesday, June 21, 2011 1:11 PM
    Friday, June 10, 2011 1:16 PM
  • I am trying to do the HttpWebRequest with ProgressBar and I need Content Length for it. Solution?
    Friday, June 10, 2011 1:41 PM
  • I am trying to do the HttpWebRequest with ProgressBar and I need Content Length for it. Solution?


    So you do have a question. 

    Since it wouldn't be worth showing a ProgressBar for subsecond downloads, tick the ProgressBar with a 1/2 or 1/3 second timer.  Then even though the download takes less than a second, it would appear to the user to take 3 to 5 seconds.

    Friday, June 10, 2011 2:48 PM
  • Oh you only wanted to know why it is -1, maybe they don't use an HTTP header on their pages. 

    Mircosoft returns 1020 

    I thought you wanted the content

    But in fact you should ask that to Google

     


    Success
    Cor
    Friday, June 10, 2011 3:23 PM