1
我知道如何捕獲HTTP響應(形式爲HttpWebResponse
)in case of an error,但當方法返回200 OK時如何執行此操作?我想要統一處理所有的答覆。如何獲得「200 OK」的HttpWebResponse?
// IService.cs
public async Task Ping();
// Client.cs
public static void ReactToHttpResponse(HttpWebResponse res)
{
// Play with the http response ...
}
await service.Ping(); // => 200 OK, on the wire
ReactToHttpResponse(/* ??? */) // How to capture the HttpWebResponse?
還是應該捕獲不同的響應類?
這隻給我狀態碼。例如,我也對標題感興趣。我意識到我可以從不同的地方聚集他們,但我認爲HttpWebResponse已經在那裏。 – 2013-02-13 08:37:42
在鏈接的文章中,HttpWebResponse引用從拋出的異常中檢索。如果函數執行時沒有觸發異常,我可以考慮這些其他可能性:1. HttpContext.Current 2.如果您正在編寫自己的HttpWebRequest對象,則應該可以調用GetResponse()方法。 – pekoponian 2013-02-13 08:56:17
在這兩種情況下,我認爲HttpWebResponse對象有一個.Headers屬性,你可以看看。 – pekoponian 2013-02-13 08:57:57