次の方法で共有


WebClient.OpenRead メソッド

指定した URI を持つリソースからダウンロードしたデータの読み取り可能ストリームを開きます。

Public Function OpenRead( _
   ByVal address As String _) As Stream
[C#]
public Stream OpenRead(stringaddress);
[C++]
public: Stream* OpenRead(String* address);
[JScript]
public function OpenRead(
   address : String) : Stream;

パラメータ

  • address
    データのダウンロード元の URI。

戻り値

リソースからデータを読み取るために使用する Stream

例外

例外の種類 条件
WebException BaseAddress および address を組み合わせて形成された URI が無効です。

または

データのダウンロード中にエラーが発生しました。

解説

OpenRead メソッドは、 address パラメータで指定したデータにアクセスするために使用する Stream インスタンスを作成します。

BaseAddress プロパティが空でない場合、 address は、相対 URI である必要があります。この相対 URI は、 BaseAddress と結合されて、要求されたデータの絶対 URI を形成します。 QueryString プロパティが null 参照 (Visual Basic では Nothing) でない場合は、 address に追加されます。

メモ    Stream で終了した場合は、システム リソースの不足を防ぐために、 Stream.Close を呼び出す必要があります。

使用例

[Visual Basic, C#, C++] uriString で識別したリソースを開き、その結果をシステム コンソールに表示する例を次に示します。データが読み込まれると、 OpenRead で返された Stream は閉じます。

 
' 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()

[C#] 
// 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();

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

Console::WriteLine(S"\nDisplaying Data :\n");
StreamReader* sr = new StreamReader(myStream);
Console::WriteLine(sr->ReadToEnd());

// Close the stream.
myStream->Close();

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, Common Language Infrastructure (CLI) Standard

参照

WebClient クラス | WebClient メンバ | System.Net 名前空間