我非常需要帮助,
我正在创建一个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; }
}
答案 0 :(得分:7)
您假设res.GetResponseStream().Read
将一次性返回整个回复。实际上,Stream.Read()
返回读取的字节数 - 如果这小于您期望的数字,则需要一直调用它,直到获取所有响应块为止。您可以在http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.getresponsestream.aspx的示例中看到这一点。
答案 1 :(得分:4)
你得到0的原因是因为阵列没有完全填满。读取不保证返回您请求的所有字节。在循环中调用它:
var numreceived = 0;
while (numreceived < numwanted)
numreceived += read(bytearray, numreceived, numwanted-numreceived);
还要进行一些错误检查(如果read返回&lt; 1,抛出IOException)。
答案 2 :(得分:0)
是的,在考虑了你的答案之后我就让它像这样工作了;
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];
int bytestoread = 0;
for (int i = 0; i < (int)res.ContentLength;)
{
bytestoread = (i + 64 < (int)res.ContentLength) ? 64 : 64 - ((i + 64) - (int)res.ContentLength);
i += res.GetResponseStream().Read(resp, i, bytestoread);
}
string data = Encoding.UTF8.GetString(resp);
FacebookFeed feed = sr.Deserialize<FacebookFeed>(data);
答案 3 :(得分:0)
此处提供的解决方案:
http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.getresponsestream.aspx
是迄今为止最简单的方法。
根据经验,这只发生在JSON的大块中,并不一定总是如此。使用上一页中提供的方法可以解决问题,但如果您的JSON响应很小,那就太过分了。