共用方式為


WebResponse.Close 方法

定義

當由子系類別覆寫時,關閉回應資料流。

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. 	
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 會清除 所使用的 WebResponse 資源,並藉由呼叫 Stream.Close 方法來關閉基礎數據流。

注意

回應必須關閉,以避免系統資源用盡。 您可以呼叫 Stream.CloseClose來關閉回應資料流。

注意

類別 WebResponse 是類別 abstract 。 運行時間實例的實際行為是由 所WebRequest.GetResponse傳回的WebResponse子系類別所決定。 如需預設值和例外狀況的詳細資訊,請參閱 子系類別的檔,例如 HttpWebResponseFileWebResponse

適用於

另請參閱