【问题标题】:String from Httpresponse not passing full value来自 Httpresponse 的字符串未传递完整值
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      你得到 0 的原因是数组没有被完全填满。读取并不能保证返回您请求的所有字节。循环调用:

      var numreceived = 0;
      while (numreceived < numwanted)
        numreceived += read(bytearray, numreceived, numwanted-numreceived);
      

      还要进行一些错误检查(如果 read 返回

      【讨论】:

        【解决方案3】:

        是的,在考虑了您的答案后,我得到了它的工作方式;

            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);
        

        【讨论】:

          【解决方案4】:

          这里提供的解决方案:

          http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.getresponsestream.aspx.

          是迄今为止最简单的方法。

          根据经验,这种情况只发生在大量 JSON 中,不一定总是如此。使用上一页中提供的方法确实可以解决问题,但如果您的 JSON 响应很小,那就有点过头了。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-10-28
            • 2013-11-23
            • 2020-06-02
            相关资源
            最近更新 更多