2010-02-26 54 views
1

我正在嘗試使用REST API從fb中獲取朋友列表。在C#中的REST API

ArrayObject的填充有:

  • SecretKey的
  • API_KEY方法= 「facebook.friends.get」
  • CALL_ID UID
  • V = 1.0
  • SIG = MD5碼

我試圖撥打電話:

public string GetResponse(ArrayObject Parameters) 
{ 
    // Set the encoding type 
    theRequest.ContentType = "application/x-www-form-urlencoded"; 

    theRequest.ContentLength = Parameters.getData().Length; 

    // We write the parameters into the request 
    StreamWriter sw = new StreamWriter(theRequest.GetRequestStream()); 
    sw.Write(Parameters.getData()); 
    sw.Flush(); 
    sw.Close(); 

    // Execute the query 
    theResponse = (HttpWebResponse)theRequest.GetResponse(); 
    StreamReader sr = new StreamReader(theResponse.GetResponseStream()); 
    return sr.ReadToEnd(); 
} 

但我來到這裏的異常:

theResponse = (HttpWebResponse) theRequest.GetResponse(); 

與消息:

The remote server returned an error: (500) Internal Server Error.

回答

0

我想這是因爲您要關閉的請求流。嘗試在收到回覆後移動

sw.Close(); 

線。

編輯:看着我的一些舊代碼。更正了我自己的答案。

+0

同樣的問題:/ – 2010-02-26 23:07:49

2

您的代碼應該是這樣的,因爲你沒有配置一些資源的正確:

public string GetResponse(ArrayObject Parameters) 
{ 
    // Set the encoding type 
    theRequest.ContentType = "application/x-www-form-urlencoded"; 

    theRequest.ContentLength = Parameters.getData().Length; 

    // We write the parameters into the request 
    using (StreamWriter sw = new StreamWriter(theRequest.GetRequestStream())) 
    { 
     sw.Write(Parameters.getData()); 
     sw.Flush(); 
    } 

    // Execute the query 
    theResponse = (HttpWebResponse)theRequest.GetResponse(); 

    using (StreamReader sr = new StreamReader(theResponse.GetResponseStream())) 
    { 
     return sr.ReadToEnd(); 
    } 
} 

而且,你似乎你緩存HttpWebResponse實例。這是一個壞主意,因爲它來自WebResponse,它實現了IDisposable。在這個實例中調用Dispose對於處理用於發出請求和讀取響應的資源非常重要。

這就是說,有沒有一個原因,你沒有使用Facebook Developer Toolkit?它包含將大多數調用封裝到RESTful API的類,以及可以重用的機制(如果有必要,可以重新生成一些代碼)來生成新調用。

+0

這不起作用... – 2010-02-26 23:29:26

+0

@C。然而,沒有跡象表明當它不起作用時會發生什麼。不是很有幫助... – casperOne 2011-01-06 02:53:45