語言

WebClient.OpenRead 方法

定義

針對從具有指定 URI 的資源下載的數據,開啟可讀取的數據流。

多載

名稱 Description
OpenRead(String)

開啟一條可讀取資料串流,存取從指定為 URI String的資源下載的資料。

OpenRead(Uri)

開啟一條可讀取資料串流,存取從指定為 URI Uri的資源下載的資料。

OpenRead(String)

來源:
WebClient.cs
來源:
WebClient.cs
來源:
WebClient.cs
來源:
WebClient.cs
來源:
WebClient.cs

開啟一條可讀取資料串流,存取從指定為 URI String的資源下載的資料。

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

URI 被指定為 String 下載資料的來源。

傳回

Stream A 用來從資源讀取資料。

例外狀況

參數 address 為 null。

由 組合BaseAddressaddress而成的 URI 是無效的。

-或-

下載數據時發生錯誤。

範例

以下程式碼範例會開啟由 uriString 識別的資源,並在系統主控台上顯示結果。 資料讀取後,返回的 Stream by 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()

備註

注意事項

WebRequest、、HttpWebRequest、ServicePointWebClient、和 都已過時,你不應該用它們來做新開發。 請改用 HttpClient。

該 OpenRead 方法建立 Stream 一個實例,用來讀取參數 address 指定的資源內容。 這個方法會在開啟數據流時封鎖。 要在等待串流時繼續執行,請使用以下 OpenReadAsync 方法之一。

若屬性 BaseAddress 不是空字串(“”)且 address 不含絕對 URI,則 address 必須是相對 URI,與 BaseAddress 結合形成請求資料的絕對 URI。 若性質 QueryString 不 null為 ,則附加於 address。

此方法會使用 RETR 命令來下載 FTP 資源。 針對 HTTP 資源,會使用 GET 方法。

Note

完成Stream.Close後必須打電話Stream,以避免系統資源耗盡。

Note

當您在應用程式中啟用網路追蹤時,此成員會輸出追蹤資訊。 欲了解更多資訊,請參閱.NET框架中的網路追蹤。

適用於

OpenRead(Uri)

來源:
WebClient.cs
來源:
WebClient.cs
來源:
WebClient.cs
來源:
WebClient.cs
來源:
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 A 用來從資源讀取資料。

例外狀況

參數 address 為 null。

由 組合BaseAddressaddress而成的 URI 是無效的。

-或-

下載數據時發生錯誤。

備註

注意事項

WebRequest、、HttpWebRequest、ServicePointWebClient、和 都已過時,你不應該用它們來做新開發。 請改用 HttpClient。

該 OpenRead 方法建立 Stream 一個實例,用來讀取參數 address 指定的資源內容。 這個方法會在開啟數據流時封鎖。 要在等待串流時繼續執行,請使用以下 OpenReadAsync 方法之一。

若屬性 BaseAddress 不是空字串(“”)且 address 不含絕對 URI,則 address 必須是相對 URI,與 BaseAddress 結合形成請求資料的絕對 URI。 若性質 QueryString 不 null為 ,則附加於 address。

此方法會使用 RETR 命令來下載 FTP 資源。 針對 HTTP 資源,會使用 GET 方法。

Note

完成Stream.Close後必須打電話Stream,以避免系統資源耗盡。

Note

當您在應用程式中啟用網路追蹤時,此成員會輸出追蹤資訊。 欲了解更多資訊,請參閱.NET框架中的網路追蹤。

適用於