【问题标题】:How to Response HTTP 504: Timer Time Out JSON如何响应 HTTP 504:定时器超时 JSON
【发布时间】:2016-05-20 08:39:03
【问题描述】:

我正在使用https://timercheck.io/YOURTIMERNAME/60 创建计时器,当计时器结束时,API 管理器会返回错误代码和一些 JSON 内容

这是定时器结束时的 JSON 数据:

{"errorMessage":"504: timer timed out"}

当定时器还在倒计时时:

{"timer":"neo308CCEACbid","request_id":"e54f484e-1e64-11e6-9552-3950b2ec2d5c","status":"ok","now":1463732937,"start_time":1463732935,"start_seconds":180,"seconds_elapsed":2,"seconds_remaining":178,"message":"Timer still running"}

由于错误代码,我在 Visual Studio 上遇到错误,并且在我的 Android 上强制关闭应用程序。我只想在 JSON 中获取 errorMessage。我正在使用 Visual Studio 2015 和 Xamarin 来制作这个项目。

提前致谢

更新:

我正在使用它来获取网络响应

 private async Task<string> FetchUserAsync(string url)
        {
            // Create an HTTP web request using the URL:
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.ContentType = "application/json";
            request.Method = "GET";

            // Send the request to the server and wait for the response:
            using (WebResponse response = await request.GetResponseAsync())
            {
                // Get a stream representation of the HTTP web response:
                using (var sr = new StreamReader(response.GetResponseStream()))
                {
                    string strContent = sr.ReadToEnd();
                    return strContent;
                }
            }
        }

然后这样称呼它:

CekTimer dataWaktu = JsonConvert.DeserializeObject<CekTimer>(await FetchUserAsync(url));

【问题讨论】:

  • 请从您的应用中发布相关代码
  • 响应的http状态码也是504吗?
  • @Jason 好的,你可以再检查一下
  • @KaiBrummund 是的,http 的响应是 504

标签: c# android api xamarin visual-studio-2015


【解决方案1】:

我假设您使用的是 HttpClientGetStringAsync,并且 HttpResponse 状态代码也是 504,就像在 Json 内容中一样。

GetStringAsync 之类的快捷方法都调用了EnsureSuccessStatusCode,这导致在 504 (see source here) 上引发异常。

你可以直接发出请求:

var client = new HttpClient();
var response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, yourUri));
var json = await response.Content.ReadAsStringAsync();

【讨论】:

    【解决方案2】:

    我找到了我的问题的答案,因为webservice返回错误代码,只需使用WebException并像这样获取StatusCode

        private async Task<string> FetchUserAsync(string url)
                    {
                        try
                        {
                           // Create an HTTP web request using the URL:
                            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                            request.ContentType = "application/json";
                            request.Method = "GET";
    
                            // Send the request to the server and wait for the response:
                            using (WebResponse response = await request.GetResponseAsync())
                            {
                                // Get a stream representation of the HTTP web response:
                                using (var sr = new StreamReader(response.GetResponseStream()))
                                {
                                    string strContent = sr.ReadToEnd();
                                    return strContent;
                                }
                            }
                        }
                        catch (WebException e)
                        {
                            string a = ((HttpWebResponse)e.Response).StatusCode.ToString();
    //Toast.MakeText(this, a, ToastLength.Long).Show();
                            if (a == "GatewayTimeout")
                            {
                                return "{'errorMessage':'504: timer timed out'}";
                            }
                            else
                            {
    
                                internetDropDialog();
                                return "";
                            }
                        }
                    }
    

    我认为这不是最好的答案,但它可以帮助你摆脱这个问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-03
      • 2020-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多