Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#facebook中的restapi_C#_Facebook_Rest - Fatal编程技术网

c#facebook中的restapi

c#facebook中的restapi,c#,facebook,rest,C#,Facebook,Rest,我正在尝试使用RESTAPI从fb获取好友列表 ArrayObject由以下内容填充: 秘钥 API_KEY Method=“facebook.friends.get” 呼叫id uid v=1.0 sig=md5代码 我正试着打电话给: public string GetResponse(ArrayObject Parameters) { // Set the encoding type theRequest.ContentType = "application/x-www

我正在尝试使用RESTAPI从fb获取好友列表

ArrayObject由以下内容填充:

  • 秘钥
  • API_KEY Method=“facebook.friends.get”
  • 呼叫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();
留言如下:

远程服务器返回了一个错误: (500)内部服务器错误


我想这是因为你正在关闭请求流。试着移动

sw.Close();
在收到回复后再行


编辑:查看我的一些旧代码。更正了我自己的答案。

您的代码应该是这样的,因为您没有正确地处理某些资源:

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();
    }
}
此外,您还可以看到您正在缓存实例。这是一个坏主意,因为它源于实现IDisposable。在此实例上调用Dispose对于释放用于发出请求和读取响应的资源非常重要


也就是说,有没有理由不使用?它包含封装RESTful API的大部分调用的类,以及在必要时可以重用以生成新调用(只需少量代码修改)的机制。

@C。然而,没有迹象表明当它不工作时会发生什么。不是很有帮助。。。