来自Httpresponse的字符串未传递完整值 [英] String from Httpresponse not passing full value

查看:51
本文介绍了来自Httpresponse的字符串未传递完整值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里迫切需要帮助,

HI i am in desperate need for help here,

我正在发出Web请求,并使用Response.ContentLenth=2246获取json字符串,但是当我将其解析为字符串时,它仅提供100个字符,因此我追踪到它仅获得小于964的值.字符串长度为仍为2246,但其余值仅为(\0) null字符.它也在下一行给出错误Unterminated string passed in. (2246):

I am making a web request and getting a json string with Response.ContentLenth=2246 but when i parse it in a string it gives only few 100 characters, i traked it down that it is only getting values less than 964. strings length is still 2246 but remaining values are just (\0) null characters. Its also giving an error Unterminated string passed in. (2246): at following line

 FacebookFeed feed = sr.Deserialize<FacebookFeed>(data);

如果响应流中的字符数少于964个字符,则效果很好.

It works fine if the response stream contains characters less than 964 chars.

以下是从最后一行遇到的完整代码错误中摘录的内容.

Following is the extract from the full code error encountered in last line.

    System.Web.Script.Serialization.JavaScriptSerializer sr = new System.Web.Script.Serialization.JavaScriptSerializer();
    System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(@"https://graph.facebook.com/100000570310973_181080451920964");
    req.Method = "GET";
    System.Net.HttpWebResponse res = (System.Net.HttpWebResponse)req.GetResponse();

    byte[] resp = new byte[(int)res.ContentLength];
    res.GetResponseStream().Read(resp, 0, (int)res.ContentLength);
    string data = Encoding.UTF8.GetString(resp);
    FacebookFeed feed = sr.Deserialize<FacebookFeed>(data);

给出的错误是

Unterminated string passed in. (2246): {"id":"100000570310973_1810804519209........ (with rest of data in the string data including null chars)

以下是我的代码中使用的类的形状:

following is the shape of classes used in my code:

public class FacebookFeed
{
    public string id { get; set; }
    public NameIdPair from { get; set; }
    public NameIdPair to { get; set; }
    public string message { get; set; }
    public Uri link{get;set;}
    public string name{get; set;}
    public string caption { get; set; }
    public Uri icon { get; set; }
    public NameLinkPair[] actions { get; set; }
    public string type { get; set; }
    public NameIdPair application { get; set; } //Mentioned in Graph API as attribution
    public DateTime created_time { get; set; }
    public DateTime updated_time { get; set; }
    public FacebookPostLikes likes { get; set; }
}

public class NameIdPair
{
    public string name { get; set; }
    public string id { get; set; }
}

public class NameLinkPair
{
    public string name { get; set; }
    public Uri link{get; set;}
}

public class FacebookPostLikes
{
    public NameIdPair[] data { get; set; }
    public int count { get; set; }
}

推荐答案

您假设res.GetResponseStream().Read将一次性返回整个响应.实际上,Stream.Read()返回已读取的字节数-如果该数目小于您期望的字节数,则需要继续调用它,直到获取所有响应块为止.您可以在 http://msdn的示例中看到此内容.microsoft.com/en-us/library/system.net.httpwebresponse.getresponsestream.aspx .

You're assuming that res.GetResponseStream().Read will return the whole response in one go. In fact, Stream.Read() returns the number of bytes that were read - if this is less than the number you're expecting, you need to keep calling it until all the response chunks have been fetched. You can see this in the example at http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.getresponsestream.aspx.

这篇关于来自Httpresponse的字符串未传递完整值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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