Export (0) Print
Expand All

WebClient.OpenRead Method

Opens a readable stream for the data downloaded from a resource with the specified URI.

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

Parameters

address
The URI to download data from.

Return Value

A Stream used to read data from a resource.

Exceptions

Exception Type Condition
WebException The URI formed by combining BaseAddress, address is invalid.

-or-

An error occurred while downloading data.

Remarks

The OpenRead method creates a Stream instance used to access the data specified by the address parameter.

If the BaseAddress property is not empty, address must be a relative URI that is combined with BaseAddress to form the absolute URI of the requested data. If the QueryString property is not a null reference (Nothing in Visual Basic), it is appended to address.

Note   You must call Stream.Close when finished with the Stream to avoid running out of system resources.

Example

[Visual Basic, C#, C++] The following example opens the resource identified by uriString and displays the results on the system console. Note that the Stream returned by OpenRead is closed when the data has been read.

[Visual Basic] 
' 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] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

See Also

WebClient Class | WebClient Members | System.Net Namespace

Show:
© 2014 Microsoft