HttpWebResponse.Close 方法

定義

關閉回應資料流。Closes the response stream.

public:
 override void Close();
public override void Close ();
override this.Close : unit -> unit
Public Overrides Sub Close ()

例外狀況

僅限 .Net Core:HttpWebResponse 物件已被處置。.NET Core only: This HttpWebResponse object has been disposed.

範例

下列範例示範如何關閉 HttpWebResponseThe following example demonstrates how to close a HttpWebResponse.

// Creates an HttpWebRequest for the specified URL.
HttpWebRequest^ myHttpWebRequest = (HttpWebRequest^)( WebRequest::Create( url ) );
// Sends the HttpWebRequest and waits for a response.
HttpWebResponse^ myHttpWebResponse = (HttpWebResponse^)( myHttpWebRequest->GetResponse() );
Console::WriteLine( "\nResponse Received::Trying to Close the response stream.." );
// Releases the resources of the response.
myHttpWebResponse->Close();
Console::WriteLine( "\nResponse Stream successfully closed" );
// Creates an HttpWebRequest for the specified URL.
    HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
    // Sends the HttpWebRequest and waits for a response.
    HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
    Console.WriteLine("\nResponse Received.Trying to Close the response stream..");
    // Releases the resources of the response.
    myHttpWebResponse.Close();
    Console.WriteLine("\nResponse Stream successfully closed");
' Creates an HttpWebRequest for the specified URL. 
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
' Sends the HttpWebRequest and waits for a response.
Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
Console.WriteLine("Response Received.Trying to Close the response stream..")
' Releases the resources of the response.
myHttpWebResponse.Close()
Console.WriteLine("Response Stream successfully closed")

備註

Close方法會關閉回應資料流程,並釋放與資源的連線,以供其他要求重複使用。The Close method closes the response stream and releases the connection to the resource for reuse by other requests.

呼叫方法之後,您不應該存取物件的任何屬性 HttpWebResponse CloseYou should not access any properties of the HttpWebResponse object after the call to the Close method. 在 .NET Core 上, ObjectDisposedException 會擲回。On .NET Core, an ObjectDisposedException is thrown.

您必須呼叫 Stream.CloseHttpWebResponse.Close 方法來關閉資料流程,並釋放連接以供重複使用。You must call either the Stream.Close or the HttpWebResponse.Close method to close the stream and release the connection for reuse. 不需要同時呼叫 Stream.CloseHttpWebResponse.Close ,但是這樣做並不會造成錯誤。It is not necessary to call both Stream.Close and HttpWebResponse.Close, but doing so does not cause an error. 無法關閉資料流程可能會導致您的應用程式連接中斷。Failure to close the stream can cause your application to run out of connections.

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。This member outputs trace information when you enable network tracing in your application. 如需詳細資訊,請參閱 .NET Framework 中的網路追蹤For more information, see Network Tracing in the .NET Framework.

適用於