WebClient.OpenRead 方法

定义

为从具有指定 URI 的资源下载的数据打开一个可读的流。

重载

OpenRead(String)

为从具有 String 指定的 URI 的资源下载的数据打开一个可读的流。

OpenRead(Uri)

为从具有 Uri 指定的 URI 的资源下载的数据打开一个可读的流。

OpenRead(String)

Source:
WebClient.cs
Source:
WebClient.cs
Source:
WebClient.cs

为从具有 String 指定的 URI 的资源下载的数据打开一个可读的流。

public:
 System::IO::Stream ^ OpenRead(System::String ^ address);
public System.IO.Stream OpenRead (string address);
member this.OpenRead : string -> System.IO.Stream
Public Function OpenRead (address As String) As Stream

参数

address
String

String 形式指定的 URI,将从中下载数据。

返回

一个 Stream,用于从资源读取数据。

例外

address 参数为 null

通过组合 BaseAddressaddress 所构成的 URI 无效。

- 或 -

下载数据时发生错误。

示例

下面的代码示例将打开 标识 uriString 的资源,并在系统控制台上显示结果。 Stream读取数据后,由 OpenRead 返回的 将关闭。

// Create a new WebClient instance.
WebClient^ myWebClient = gcnew WebClient;
// Download home page data.
Console::WriteLine( "Accessing {0} ...", uriString );
// Open a stream to point to the data stream coming from the Web resource.
Stream^ myStream = myWebClient->OpenRead( uriString );

Console::WriteLine( "\nDisplaying Data :\n" );
StreamReader^ sr = gcnew StreamReader( myStream );
Console::WriteLine( sr->ReadToEnd() );

// Close the stream.
myStream->Close();
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
// Download home page data. 
Console.WriteLine("Accessing {0} ...",  uriString);						
// Open a stream to point to the data stream coming from the Web resource.
Stream myStream = myWebClient.OpenRead(uriString);

Console.WriteLine("\nDisplaying Data :\n");
StreamReader sr = new StreamReader(myStream);
Console.WriteLine(sr.ReadToEnd());

// Close the stream. 
myStream.Close();
    ' Create a new WebClient instance.
    Dim myWebClient As New WebClient()

    ' Download home page data. 
    Console.WriteLine("Accessing {0} ...", uriString)

    ' Open a stream to point to the data stream coming from the Web resource.
    Dim myStream As Stream = myWebClient.OpenRead(uriString)

    Console.WriteLine(ControlChars.Cr + "Displaying Data :" + ControlChars.Cr)
Dim sr As New StreamReader(myStream)
Console.WriteLine(sr.ReadToEnd())


    ' Close the stream.
    myStream.Close()

注解

方法 OpenRead 创建一个 Stream 实例,该实例用于读取 由 address 参数指定的资源的内容。 此方法在打开流时阻止。 若要在等待流时继续执行,请使用 方法之 OpenReadAsync 一。

BaseAddress如果属性不是空字符串 (“”) ,并且address不包含绝对 URI,address则必须是一个相对 URI,与 BaseAddress 组合以构成所请求数据的绝对 URI。 QueryString如果该属性不是 null,则将其追加到 address

此方法使用 RETR 命令下载 FTP 资源。 对于 HTTP 资源,使用 GET 方法。

注意

使用 完成后Stream,必须调用 Stream.Close 以避免系统资源耗尽。

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的网络跟踪

适用于

OpenRead(Uri)

Source:
WebClient.cs
Source:
WebClient.cs
Source:
WebClient.cs

为从具有 Uri 指定的 URI 的资源下载的数据打开一个可读的流。

public:
 System::IO::Stream ^ OpenRead(Uri ^ address);
public System.IO.Stream OpenRead (Uri address);
member this.OpenRead : Uri -> System.IO.Stream
Public Function OpenRead (address As Uri) As Stream

参数

address
Uri

Uri 形式指定的 URI,将从中下载数据。

返回

一个 Stream,用于从资源读取数据。

例外

address 参数为 null

通过组合 BaseAddressaddress 所构成的 URI 无效。

- 或 -

下载数据时发生错误。

注解

方法 OpenRead 创建一个 Stream 实例,该实例用于读取 由 address 参数指定的资源的内容。 此方法在打开流时阻止。 若要在等待流时继续执行,请使用 方法之 OpenReadAsync 一。

BaseAddress如果属性不是空字符串 (“”) ,并且address不包含绝对 URI,address则必须是一个相对 URI,与 BaseAddress 组合以构成所请求数据的绝对 URI。 QueryString如果该属性不是 null,则将其追加到 address

此方法使用 RETR 命令下载 FTP 资源。 对于 HTTP 资源,使用 GET 方法。

注意

使用 完成后Stream,必须调用 Stream.Close 以避免系统资源耗尽。

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的网络跟踪

适用于