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