当文件太大时,C#HttpWebResponse contentlength = -1

use*_*899 2 c# httpwebrequest httpwebresponse

我从烂番茄网站获得了json格式的字符串.我的代码看起来像

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
}
Run Code Online (Sandbox Code Playgroud)

当我运行一个返回1-4电影的电影搜索时它工作正常.但是,如果我尝试获得5或更多的结果,它将无法正常工作.webResponse内容长度为-1.当我返回4部电影的结果时,内容长度为7,449.

spe*_*der 8

当contentLength返回-1时,这很可能是因为响应是以分块传输编码(或可能是http"0.9")返回的.因此,在传输开始时没有已知的内容长度.只需阅读您的信息StreamReader直到最后,您将获得服务器发送给您的所有信息.