2010-12-27 4 views
1

HI,문자열 전체 가치를 내가 여기에 도움을 절실히 필요 해요

나는 웹 요청을 만드는 중이을 전달하고 Response.ContentLenth=2246와 JSON 문자열을 받고하지만 난 문자열을 구문 분석 할 때 100 만 몇 준다 문자, 나는 964보다 작은 값을 얻는다는 것을 추적했다. 문자열 길이는 여전히 2246이지만 나머지 값은 단지 (\0)null 문자이다. 그것 또한 다음 줄

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

응답 스트림이 964 개 문자보다 적은 문자가 포함 된 경우 그것은 잘 작동에 오류 Unterminated string passed in. (2246):을 제공합니다.

다음은 마지막 줄에서 발생한 전체 코드 오류에서 추출한 것입니다. 주어진

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

답변

4

배열이 완전히 작성되지 때문에 공의를 얻고있는 이유는 . read는, 요구 한 모든 바이트를 돌려주는 것을 보증하지 않습니다. 루프에 전화 :

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

은 또한 어떤 에러 체크에 넣어 (반환 < 1을 읽으면, IOException를 슬로우).

+0

오 !! 나는 그것을 생각하지 않았다. 고맙습니다. –

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); 
관련 문제