次の方法で共有


WebResponse.Close メソッド

派生クラスでオーバーライドされると、応答ストリームを閉じます。

Public Overridable Sub Close()
[C#]
public virtual void Close();
[C++]
public: virtual void Close();
[JScript]
public function Close();

例外

例外の種類 条件
NotSupportedException メソッドが派生クラスでオーバーライドされていないのに、そのメソッドへのアクセスが試行されました。

解説

Close メソッドは、 WebResponse によって使用されたリソースをクリーンアップし、 Stream.Close メソッドを呼び出して、元になるストリームを閉じます。

メモ   システム リソースの不足を防ぐために、応答を閉じる必要があります。応答ストリームは、 Stream.Close または Close を呼び出して、閉じることができます。

メモ    WebResponse クラスは、抽象 (Visual Basic では MustInherit) クラスです。実行時の WebResponse インスタンスの実際の動作は、 WebRequest.GetResponse で返される派生クラスによって決まります。既定値および例外の詳細については、 HttpWebResponseFileWebResponse などの派生クラスの説明を参照してください。

使用例

[Visual Basic, C#, C++] Close メソッドを使用して、WebResponse を閉じる例を次に示します。

 
' 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")

[C#] 
// Create a 'WebRequest' object with the specified url.     
WebRequest myWebRequest = WebRequest.Create("https://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");

[C++] 
// Create a 'WebRequest' object with the specified url.
WebRequest* myWebRequest = WebRequest::Create(S"https://www.contoso.com");
// Send the 'WebRequest' and wait for response.
WebResponse* myWebResponse = myWebRequest->GetResponse();

// Process the response here.
Console::WriteLine(S"\nResponse Received::Trying to Close the response stream..");
// Release resources of response Object*.
myWebResponse->Close();
Console::WriteLine(S"\nResponse Stream successfully closed");

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

WebResponse クラス | WebResponse メンバ | System.Net 名前空間 | ネットワークでのストリームの使用