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 。 实例在运行时的实际行为 WebResponse 由 返回 WebRequest.GetResponse的后代类确定。 有关默认值和异常的详细信息,请参阅后代类的文档,例如 HttpWebResponseFileWebResponse

适用于

另请参阅