WebResponse.Close 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
當後代類別覆蓋時,會關閉回應串流。
public:
virtual void Close();
public virtual void Close();
abstract member Close : unit -> unit
override this.Close : unit -> unit
Public Overridable Sub Close ()
例外狀況
在子系類別中未覆寫 方法時,會嘗試存取方法。
範例
以下範例使用 Close 閉合 WebResponse的方法。
// Create a 'WebRequest' object with the specified url.
WebRequest myWebRequest = WebRequest.Create("http://www.contoso.com");
// Send the 'WebRequest' and wait for response.
WebResponse myWebResponse = myWebRequest.GetResponse();
// Process the response here.
Console.WriteLine("\nResponse Received.Trying to Close the response stream..");
// Release resources of response object.
myWebResponse.Close();
Console.WriteLine("\nResponse Stream successfully closed");
' Create a 'WebRequest' object with the specified url
Dim myWebRequest As WebRequest = WebRequest.Create("www.contoso.com")
' Send the 'WebRequest' and wait for response.
Dim myWebResponse As WebResponse = myWebRequest.GetResponse()
' Process the response here
Console.WriteLine(ControlChars.Cr + "Response Received.Trying to Close the response stream..")
' Release resources of response object
myWebResponse.Close()
Console.WriteLine(ControlChars.Cr + "Response Stream successfully closed")
備註
該 Close 方法會清理 a WebResponse 所使用的資源,並透過呼叫該 Stream.Close 方法關閉底層串流。
Note
回應必須關閉,以避免系統資源耗盡。 回應串流可透過呼叫 Stream.Close 或 Close封閉。
Note
這 WebResponse 堂課就是一 abstract 堂課。 執行時實例的實際行為 WebResponse 由回 WebRequest.GetResponse傳的後代類別決定。 如需更多關於預設值與例外的資訊,請參閱後代類別的文件,如 HttpWebResponse 和 FileWebResponse。