HttpWebResponse Status Code 429

China☆狼群 提交于 2019-12-25 09:25:24

问题


I'm doing some integration work with an API which returns a HttpStatusCode 429 when you've hit your daily limit. However the Enum HttpStatusCode in the web response object does not contain this code.

Can some one let me know how I can check for this response code?

Here is some code to show what I'm trying to accomplish:

try
{
  //do something
}
catch (WebException webExp)
{
     var response = (HttpWebResponse) webExp.Response;

     //here I want to check status code for too many requests but it is not   in the enum.
    if (response.StatusCode == HttpStatusCode.TooManyRequests) throw webExp;
}

回答1:


I had the same problem, you can read the response and then look for 429 or too many requests string:

string responseStr = "";
Stream responseStream = webEx.Response.GetResponseStream();
if (responseStream != null)
{
    using (StreamReader sr = new StreamReader(responseStream))
    {
       responseStr = sr.ReadToEnd();
    }
}

if (responseStr.Contains("429") || responseStr.Contains("Too many requests"))
Console.WriteLine("Is 429 WebException ");


来源:https://stackoverflow.com/questions/43373871/httpwebresponse-status-code-429

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!