2017-04-12 70 views
0

我正在做一些API的集成工作,當你達到每日限制時它會返回一個HttpStatusCode 429。但是,Web響應對象中的枚舉HttpStatusCode不包含此代碼。HttpWebResponse狀態代碼429

有人可以讓我知道如何檢查此響應代碼嗎?

下面是一些代碼來證明什麼,我試圖完成:

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; 
} 
+0

請張貼您的代碼,以便我們可以提出建議。現在我們不知道你是如何調用api的。 – Lorien

+0

沒有代碼示例很難提供幫助;你看過[如何返回429](http://stackoverflow.com/questions/22636602/how-to-return-http-429)並向後工作。 –

+0

道歉,我已經添加了一個代碼示例來幫助解釋我正在嘗試做什麼。 – kcis8rm

回答

0

我有同樣的問題,你可以讀取響應,然後找429個或過多的請求字符串:

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 ");