文件太大时C#HttpWebResponse contentlength = -1 [英] C# HttpWebResponse contentlength = -1 when file too large

查看:255
本文介绍了文件太大时C#HttpWebResponse contentlength = -1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Rotten Tomatoes网站获取json格式的字符串.我的代码看起来像

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
}

当我运行返回1-4部电影的电影搜索时,效果很好.但是,如果我尝试获得5个或更多的结果,它将不起作用. webResponse的内容长度为-1.当我返回4部电影的结果时,内容长度为7,449.

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.

推荐答案

当contentLength返回-1时,最有可能因为在StreamReader直到结尾,您就可以将服务器发送给您的所有信息.

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.

这篇关于文件太大时C#HttpWebResponse contentlength = -1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆