WebClient.OpenRead 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
針對從具有指定 URI 的資源下載的數據,開啟可讀取的數據流。
多載
| 名稱 | Description |
|---|---|
| OpenRead(String) |
使用指定為 String的 URI,開啟從資源下載之數據的可讀取數據流。 |
| OpenRead(Uri) |
使用指定為 Uri的 URI,開啟從資源下載之數據的可讀取數據流。 |
OpenRead(String)
- 來源:
- WebClient.cs
- 來源:
- WebClient.cs
- 來源:
- WebClient.cs
- 來源:
- 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
參數
傳回
用來從資源讀取數據的 Stream。
例外狀況
address 參數是 null。
範例
下列程式代碼範例會開啟 uriString 所識別的資源,並在系統控制台上顯示結果。 讀取數據時,OpenRead 所傳回的 Stream 會關閉。
// 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、ServicePoint和 WebClient 已經過時,您不應該將它們用於新的開發。 請改用 HttpClient。
OpenRead 方法會建立 Stream 實例,用來讀取 address 參數所指定之資源的內容。 這個方法會在開啟數據流時封鎖。 若要在等候數據流時繼續執行,請使用其中一個 OpenReadAsync 方法。
如果 BaseAddress 屬性不是空字串 (“”) 且 address 不包含絕對 URI,address 必須是與 BaseAddress 結合的相對 URI,才能形成所要求數據的絕對 URI。 如果 QueryString 屬性不是 null,則會附加至 address。
此方法會使用 RETR 命令來下載 FTP 資源。 針對 HTTP 資源,會使用 GET 方法。
注意
完成 Stream 時,您必須呼叫 Stream.Close,以避免系統資源用盡。
注意
當您在應用程式中啟用網路追蹤時,此成員會輸出追蹤資訊。 如需詳細資訊,請參閱 .NET Framework 中的網路追蹤。
適用於
OpenRead(Uri)
- 來源:
- 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
參數
傳回
用來從資源讀取數據的 Stream。
例外狀況
address 參數是 null。
備註
謹慎
WebRequest、HttpWebRequest、ServicePoint和 WebClient 已經過時,您不應該將它們用於新的開發。 請改用 HttpClient。
OpenRead 方法會建立 Stream 實例,用來讀取 address 參數所指定之資源的內容。 這個方法會在開啟數據流時封鎖。 若要在等候數據流時繼續執行,請使用其中一個 OpenReadAsync 方法。
如果 BaseAddress 屬性不是空字串 (“”) 且 address 不包含絕對 URI,address 必須是與 BaseAddress 結合的相對 URI,才能形成所要求數據的絕對 URI。 如果 QueryString 屬性不是 null,則會附加至 address。
此方法會使用 RETR 命令來下載 FTP 資源。 針對 HTTP 資源,會使用 GET 方法。
注意
完成 Stream 時,您必須呼叫 Stream.Close,以避免系統資源用盡。
注意
當您在應用程式中啟用網路追蹤時,此成員會輸出追蹤資訊。 如需詳細資訊,請參閱 .NET Framework 中的網路追蹤。