共用方式為


HttpWebResponse.Close 方法

定義

關閉回應資料流。

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

例外狀況

僅限 .NET Core:HttpWebResponse 物件已處置。

範例

下列範例示範如何關閉 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 會關閉回應數據流,並釋放與資源的連接,以供其他要求重複使用。

在呼叫 Close 方法之後,您不應該存取 物件的任何屬性HttpWebResponse。 在 .NET Core 上, ObjectDisposedException 會擲回 。

您必須呼叫 Stream.CloseHttpWebResponse.Close 方法來關閉數據流,並釋放連線以供重複使用。 不需要同時呼叫 Stream.CloseHttpWebResponse.Close,但這麼做不會造成錯誤。 無法關閉資料流可能會導致您的應用程式用盡連線。

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。 如需詳細資訊,請參閱 .NET Framework 中的網路追蹤

適用於