2011-03-18 18 views
0

所以我試圖使用新的「批處理」功能到圖形API,其描述爲here。我認爲問題出在我用POST提交數據的方式,而且我很難調試它。這可能是一個JSON問題,但我不這麼認爲。 這裏的C#C#實現Facebook的新「批處理」功能

 HttpWebRequest httpRequest =(HttpWebRequest)WebRequest.Create("https://graph.facebook.com/"); 
     httpRequest.Method = "POST"; 
     httpRequest.ContentType = "application/x-www-form-urlencoded"; 

     byte[] bytedata = Encoding.UTF8.GetBytes(o.ToString()); 
     httpRequest.ContentLength = bytedata.Length; 

     Stream requestStream = httpRequest.GetRequestStream(); 
     requestStream.Write(bytedata, 0, bytedata.Length); 
     requestStream.Close(); 

     HttpWebResponse httpWebResponse = (HttpWebResponse)httpRequest.GetResponse(); 
     Stream responseStream = httpWebResponse.GetResponseStream(); 
     StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.UTF8); 

     string APIData = reader.ReadToEnd(); 
     JObject MyApiData = JObject.Parse(APIData); 

,並且變量 「O」 包含以下JSON:

{ 
    "access_token": "[my real token]", 
    "batch": [ 
    { 
     "method": "get", 
     "relative_url": "me" 
    }, 
    { 
     "method": "get", 
     "relative_url": "me/friends" 
    } 
    ] 
} 

任何想法我做錯了嗎?它實際上輸出Facebook的開發者網站上的文檔....所以我認爲這是一個線索,它搞砸了;-)

回答

3

嘗試了這一點: 私人無效PostBatch(字符串_token) {

 string p1 = "access_token=" + Server.UrlEncode(_token); 
     string p2 = "&batch=" + Server.UrlEncode(" [ { \"method\": \"get\", \"relative_url\": \"me\" }, { \"method\": \"get\", \"relative_url\": \"me/friends\" } ]"); 

     try 
     { 

      HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create("https://graph.facebook.com/"); 
      httpRequest.Method = "POST"; 
      httpRequest.ContentType = "application/x-www-form-urlencoded"; 

      byte[] bytedata = Encoding.UTF8.GetBytes(p1 + p2); 
      httpRequest.ContentLength = bytedata.Length; 

      Stream requestStream = httpRequest.GetRequestStream(); 
      requestStream.Write(bytedata, 0, bytedata.Length); 
      requestStream.Close(); 

      HttpWebResponse httpWebResponse = (HttpWebResponse)httpRequest.GetResponse(); 
      Stream responseStream = httpWebResponse.GetResponseStream(); 
      StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.UTF8); 

      string APIData = reader.ReadToEnd(); 
      Response.Write(APIData); 
     } 
     catch (Exception ex) 
     { Response.Write(ex.Message.ToString() + "<br>"); } 
     // JObject MyApiData = JObject.Parse(APIData); 


    } 
+0

路去吧,蒂姆!非常好,非常感謝你的幫助。 – kevin 2011-06-02 04:29:10

+0

沒問題 - 在facebook上認識某人,幫助我瞭解curl -F是什麼...... :) – 2011-06-02 18:30:49