【发布时间】:2010-12-27 14:51:33
【问题描述】:
您好,我在这里急需帮助,
我正在发出一个 Web 请求并获取一个带有 Response.ContentLenth=2246 的 json 字符串,但是当我将它解析为一个字符串时,它只给出了几个 100 个字符,我认为它只得到小于 964 的值。字符串长度是仍然是 2246,但剩余的值只是 (\0) null 字符。它还在以下行给出错误Unterminated string passed in. (2246):
FacebookFeed feed = sr.Deserialize<FacebookFeed>(data);
如果响应流包含少于 964 个字符的字符,它可以正常工作。
以下是最后一行遇到的完整代码错误的摘录。
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)
以下是我的代码中使用的类的形状:
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; }
}
【问题讨论】:
标签: c# string null httpresponse