1

我正在尝试使用以下代码更新 Facebook 排行榜上的用户分数:

string _url = "https://graph.facebook.com/MY_APP_ID/scores";
var _parameters="score=100&access_token=MY_APP_TOKEN_WITH_PUBLISH_ACTION";
WebRequest _request = WebRequest.Create(_url);
_request.Method = "POST";                 
var _dataArray = Encoding.ASCII.GetBytes(_parameters.ToString());                
_request.ContentLength = _dataArray.Length;

using (Stream _dataStream = _request.GetRequestStream())
{
    _dataStream.Write(_dataArray, 0, _dataArray.Length);
}
WebResponse _response = _request.GetResponse();

当我尝试获取响应时,应用程序会引发异常:

远程服务器返回错误:(400) 错误请求。

我究竟做错了什么?

4

1 回答 1

1

实际上,我正处于与 LinkedIn 类似的事情中。将其包装在 try 块中并尝试此异常处理程序:

catch (WebException webEx) {
    StringBuilder sb = new StringBuilder();

    sb.AppendLine(webEx.Message);

    sb.AppendLine();
    sb.AppendLine("REQUEST: ");
    sb.AppendLine();

    sb.AppendLine(string.Format("Request URL: {0} {1}", webRequest.Method, webRequest.Address));
    sb.AppendLine("Headers:");
    foreach (string header in webRequest.Headers) {
        sb.AppendLine(header + ": " + webRequest.Headers[header]);
    }

    sb.AppendLine();
    sb.AppendLine("RESPONSE: ");
    sb.AppendLine();

    sb.AppendLine(string.Format("Status: {0}", webEx.Status));

    if (null != webEx.Response) {
        HttpWebResponse response = (HttpWebResponse) webEx.Response;

        sb.AppendLine(string.Format("Status Code: {0} {1}", (int) response.StatusCode, response.StatusDescription));
        if (0 != webEx.Response.ContentLength) {
            using (var stream = webEx.Response.GetResponseStream()) {
                if (null != stream) {
                    using (var reader = new StreamReader(stream)) {
                        sb.AppendLine(string.Format("Response: {0}", reader.ReadToEnd()));
                    }
                }
            }
        }
    }

    _log.Warn(sb.ToString(), webEx);

    throw new Exception(webEx.Message, webEx);
}

这至少会记录他们返回的内容。就我而言,我无法在我的系统上重现该问题,但 QA 的服务器收到了大量的 400 个错误请求。结果发现它们是无效的时间戳,因为系统时钟慢了 7 分钟,LinkedIn 拒绝了时间戳。

于 2012-10-02T17:04:26.380 回答