C# HttpWebResponse contentlength = -1 when file to

2019-05-21 07:59发布

问题:

I'm getting a string in a json format from the Rotten Tomatoes website. My code looks like

HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(url);
webRequest.Method = "GET";
webRequest.ContentType = "application/json";

HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

using(StreamReader reader = new StreamReader(response.GetResponseStream()))
{
    //Code I'm using the reader with
}

When I run a movie search that returns 1 - 4 movies it works fine. However, If I try to get the results of 5 or more it won't work. The webResponse contentlength is -1. When I'm returning the results of 4 movies the contentlength is 7,449.

回答1:

When contentLength returns -1, this is most likely because the response is being returned in chunked transfer encoding (or possibly http "0.9"). As such, there is no known content-length at the start of the transmission. Just read your StreamReader until the end, and you'll have everything the server sent to you.



回答2:

Expected behavior - property returns content length as set by the server, so if that header is not set you get -1 (which is likely behavior for large files that are streamed from server).

HttpWebResponse.ContentLength:

Remarks: 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