问题
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