【问题标题】:REST API in c# facebookc# facebook 中的 REST API
【发布时间】:2010-02-26 22:37:46
【问题描述】:

我正在尝试使用 REST API 从 fb 获取好友列表。

ArrayObject 填充:

  • 密钥
  • 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();

带有以下信息:

远程服务器返回错误: (500) 内部服务器错误。

【问题讨论】:

    标签: c# facebook rest


    【解决方案1】:

    您的代码应该如下所示,因为您没有正确处理某些资源:

    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 实例。这是一个坏主意,因为它源自实现 IDisposable 的 WebResponse。在此实例上调用 Dispose 很重要,以便释放用于发出请求和读取响应的资源。

    话虽如此,您不使用Facebook Developer Toolkit 有什么原因吗?它包含封装了对 RESTful API 的大部分调用的类,以及您可以在必要时重用以生成新调用(只需进行少量代码修改)的机制。

    【讨论】:

    • @C.然而,当它不起作用时,没有迹象表明你会发生什么。不是很有帮助...
    【解决方案2】:

    我认为这是因为您正在关闭请求流。尝试移动

    sw.Close();
    

    收到回复后的一行。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-30
      • 2011-02-18
      • 1970-01-01
      • 2017-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多