如何在 Windows Phone 开发中使用 Twitter 错误代码响应



>im 使用推特 API 搜索用户名,但是我想对错误代码响应进行一些错误处理。这是我正在寻找代码 https://dev.twitter.com/docs/error-codes-responses 的网站

我想抓住这些可能的错误。我正在尝试

  catch (Exception error)
        {
          //do thing

        }

但我似乎无法填补空白。有人可以帮助我,例如,如果代码 404 发生,那么它将显示该错误代码的消息。谢谢

处理取决于您使用的 HTTP 库。如果是 WebClient 或 HttpWebRequest,则可以捕获 WebException 并检查 Response 属性的 StatusCode 属性。例如

((HttpWebResponse)e.Response).StatusCode

如果您使用的是HttpClient(这是我个人的偏好),您将从PostAsync或SendAsync等查询中返回HttpResponseMessage。你可以从该HttpResponseMessage中读取StatusCode,以了解Twitter的回应。下面是 LINQ to Twitter 错误处理程序(正在进行中):

using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using LinqToTwitter.Common;
using LitJson;
namespace LinqToTwitter
{
    class TwitterErrorHandler
    {
        public static async Task ThrowIfErrorAsync(HttpResponseMessage msg)
        {
            // TODO: research proper handling of 304
            if ((int)msg.StatusCode < 400) return;
            switch (msg.StatusCode)
            {
                case HttpStatusCode.Unauthorized:
                    await HandleUnauthorizedAsync(msg);
                    break;
                default:
                    await HandleGenericErrorAsync(msg);
                    break;
            } 
        }
        internal static async Task HandleGenericErrorAsync(HttpResponseMessage msg)
        {
            string responseStr = await msg.Content.ReadAsStringAsync();
            BuildAndThrowTwitterQueryException(responseStr, msg);
        }
        internal static void BuildAndThrowTwitterQueryException(string responseStr, HttpResponseMessage msg)
        {
            TwitterErrorDetails error = ParseTwitterErrorMessage(responseStr);
            throw new TwitterQueryException(error.Message)
            {
                ErrorCode = error.Code,
                StatusCode = msg.StatusCode,
                ReasonPhrase = msg.ReasonPhrase
            };
        }
        internal async static Task HandleUnauthorizedAsync(HttpResponseMessage msg)
        {
            string responseStr = await msg.Content.ReadAsStringAsync();
            TwitterErrorDetails error = ParseTwitterErrorMessage(responseStr);
            string message = error.Message + " - Please visit the LINQ to Twitter FAQ (at the HelpLink) for help on resolving this error.";
            throw new TwitterQueryException(message)
            {
                HelpLink = "https://linqtotwitter.codeplex.com/wikipage?title=LINQ%20to%20Twitter%20FAQ",
                ErrorCode = error.Code,
                StatusCode = HttpStatusCode.Unauthorized,
                ReasonPhrase = msg.ReasonPhrase
            };
        }
        internal static TwitterErrorDetails ParseTwitterErrorMessage(string responseStr)
        {
            if (responseStr.StartsWith("{"))
            {
                JsonData responseJson = JsonMapper.ToObject(responseStr);
                var errors = responseJson.GetValue<JsonData>("errors");
                if (errors != null)
                {
                    if (errors.GetJsonType() == JsonType.String)
                        return new TwitterErrorDetails
                        {
                            Message = responseJson.GetValue<string>("errors"),
                            Code = -1
                        };
                    if (errors.Count > 0)
                    {
                        var error = errors[0];
                        return new TwitterErrorDetails
                        {
                            Message = error.GetValue<string>("message"),
                            Code = error.GetValue<int>("code")
                        };
                    }
                }
            }
            return new TwitterErrorDetails { Message = responseStr };
        }
        internal class TwitterErrorDetails
        {
            public int Code { get; set; }
            public string Message { get; set; }
        }
    }
}

最新更新