C# HttpWebResponse contentlength = -1 when file too large

自作多情 提交于 2019-12-22 15:00:07

问题


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



来源:https://stackoverflow.com/questions/15375499/c-sharp-httpwebresponse-contentlength-1-when-file-too-large

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!