[英]Check null for HttpWebResponse
我正在向 REST 服務發出 HTTP post 請求,當我得到 HttpWebResponse 時,我正在做以下檢查。 當我在做 webresponse 時,我是否也應該檢查 responseStream != null
HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;
if (webResponse != null)
{
var responseStream = webResponse.GetResponseStream();
int responseCode = (int)webResponse.StatusCode;
if (responseStream != null && responseCode == (int)HttpStatusCode.Created)
{
cmsStoreWebResponse = ((new StreamReader(responseStream)).ReadToEnd());`
}
else
{
this.LogError(string.Format("{0}\n Endpoint: {1}\n {2} {3} {4}", ErrorCodes.IWS_CMSRetrieve_ERROR_001, oagEndpointUrl, ErrorCodes.IWS_CMSStore_ERROR_SERVICE_DOWN, responseStream, responseCode));
serviceData.Fatal = true;
serviceData.ErrorCode = ErrorCodes.IWS_EFORMSFORMSETS_001;
serviceData.ErrorDetails = string.Format("\nEndpoint: {0}\n {1}", oagEndpointUrl, ErrorCodes.RESPONSE_STREAM_NULL);
throw new FaultException<ServiceExceptionData>(serviceData, new FaultReason(string.Format("\nEndpoint: {0}\n {1}", oagEndpointUrl, ErrorCodes.RESPONSE_STREAM_NULL)));
}
}
else
{
this.LogError(string.Format("{0}\n Endpoint: {1}\n {2}", ErrorCodes.IWS_CMSRetrieve_ERROR_001, oagEndpointUrl, ErrorCodes.IWS_CMSStore_ERROR_SERVICE_DOWN));
serviceData.Fatal = true;
serviceData.ErrorCode = ErrorCodes.IWS_EFORMSFORMSETS_001;
serviceData.ErrorDetails = string.Format("\nEndpoint: {0}\n {1}", oagEndpointUrl, ErrorCodes.RESPONSE_STREAM_NULL);
throw new FaultException<ServiceExceptionData>(serviceData, new FaultReason(string.Format("\nEndpoint: {0}\n {1}", oagEndpointUrl, ErrorCodes.RESPONSE_STREAM_NULL)));
}
沒有從WebResponse
派生的內置類型,特別是HttpWebResponse
,可以返回null。 這種迷信的信念誤導了許多開發者。 不要檢查null。 這樣的代碼是死代碼。
與返回空流相比,什么是null甚至意味着什么?! 這根本不符合邏輯。
此外, GetResponse()
不能返回null。 那又是什么意思?! HTTP協議不支持“空響應”的概念。 如果由於庫錯誤而發生這種情況,那么無論如何都無法處理這種情況。 任何此類檢查都無濟於事。
可以創建從WebResponse
派生的類,這些類返回諸如null
類的瘋狂值。 沒有內置類可以做到這一點,它應該被視為返回null
的錯誤。 從WebResponse
派生的類非常罕見。 我從未見過一個。
這是一個很好的代碼模式:
var request = WebRequest.Create("http://example.org/");
using (var response = request.GetResponse())
using (var responseStream = response.GetResponseStream())
using (var responseReader = new StreamReader(responseStream))
{
var contents = responseReader.ReadToEnd();
}
它演示了如何使用HttpWebRequest
簡潔安全地讀取HTTP URL的內容。
HttpWebResponse.GetResponseStream()
可以返回null,所以是的,你應該檢查它是否為null。
您還應該檢查HttpWebResponse.GetResponseStream() == Stream.Null
所以,像這樣:
var webResponseStream = webResponse.GetResponseStream();
if (webResponseStream != null && webResponseStream != Stream.Null)
{
//do stuff.
}
如果您想知道, webResponseStream != null
檢查是否已將參考分配給變量webResponseStream
,而webResponseStream != Stream.Null
檢查分配給webResponseStream
的Stream實例是否包含任何后備數據。
你看起來不太對勁。 C#表格:
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
我的第一個Web請求正常傳遞,第二個返回null
。 我甚至不知道錯誤在哪里。 很明顯它在代碼中。 而且C#代碼也有自己的bug。
myRequestState.response.Close(); //myRequestState.response==null !!!
您不考慮標題,多線程等等。我建議您嘗試自己編寫Web處理程序並從服務器返回null
。 Finnaly null
在
sysnem.threeding.nManualResetEvent allDone
有必要添加:
allDone.Reset();
enter code here
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.