FileWebResponse.Close 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
关闭响应流。
public:
override void Close();
public override void Close ();
override this.Close : unit -> unit
Public Overrides Sub Close ()
示例
以下示例使用 Close 方法关闭响应流。
void GetPage( String^ url )
{
try
{
Uri^ fileUrl = gcnew Uri( String::Concat( "file://", url ) );
// Create a FileWebrequest with the specified Uri.
FileWebRequest^ myFileWebRequest = dynamic_cast<FileWebRequest^>(WebRequest::Create( fileUrl ));
// Send the 'fileWebRequest' and wait for response.
FileWebResponse^ myFileWebResponse = dynamic_cast<FileWebResponse^>(myFileWebRequest->GetResponse());
// Process the response here.
Console::WriteLine( "\nResponse Received::Trying to Close the response stream.." );
// Release resources of response Object*.
myFileWebResponse->Close();
Console::WriteLine( "\nResponse Stream successfully closed." );
}
catch ( WebException^ e )
{
Console::WriteLine( "\r\nWebException thrown. The Reason for failure is : {0}", e->Status );
}
catch ( Exception^ e )
{
Console::WriteLine( "\nThe following Exception was raised : {0}", e->Message );
}
}
public static void GetPage(String url)
{
try
{
Uri fileUrl = new Uri("file://"+url);
// Create a FileWebrequest with the specified Uri.
FileWebRequest myFileWebRequest = (FileWebRequest)WebRequest.Create(fileUrl);
// Send the 'fileWebRequest' and wait for response.
FileWebResponse myFileWebResponse = (FileWebResponse)myFileWebRequest.GetResponse();
// Process the response here.
Console.WriteLine("\nResponse Received.Trying to Close the response stream..");
// Release resources of response object.
myFileWebResponse.Close();
Console.WriteLine("\nResponse Stream successfully closed.");
}
catch(WebException e)
{
Console.WriteLine("\r\nWebException thrown. The Reason for failure is : {0}",e.Status);
}
catch(Exception e)
{
Console.WriteLine("\nThe following Exception was raised : {0}",e.Message);
}
}
Public Shared Sub GetPage(url As [String])
Try
Dim fileUrl As New Uri("file://" + url)
' Create a FileWebrequest with the specified Uri.
Dim myFileWebRequest As FileWebRequest = CType(WebRequest.Create(fileUrl), FileWebRequest)
' Send the 'fileWebRequest' and wait for response.
Dim myFileWebResponse As FileWebResponse = CType(myFileWebRequest.GetResponse(), FileWebResponse)
' Process the response here
Console.WriteLine(ControlChars.Cr + "Response Received.Trying to Close the response stream..")
' The method call to release resources of response object.
myFileWebResponse.Close()
Console.WriteLine(ControlChars.Cr + "Response Stream successfully closed")
Catch e As WebException
Console.WriteLine(ControlChars.Lf + ControlChars.Cr + "The Reason for failure is : {0}", e.Status)
Catch e As Exception
Console.WriteLine(ControlChars.Cr + "The following exception was raised : {0}", e.Message)
End Try
注解
方法 Close 清理 使用的资源, FileWebResponse 并通过调用 Stream.Close 方法来关闭响应流。
注意
必须关闭响应流,以避免系统资源耗尽。 可以通过调用 Stream.Close 或 来关闭响应流 Close