2010-02-26 53 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