Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to read from a url into a System.IO.Stream object?

I am attempting to read from a url into a System.IO.Stream object. I tried to use

Dim stream as Stream = New FileStream(msgURL, FileMode.Open)

but I get an error that URI formats are not supported with FileStream objects. Is there some method I can use that inherits from System.IO.Stream that is able to read from a URL?

like image 645
swolff1978 Avatar asked Aug 03 '09 16:08

swolff1978


People also ask

What is a system IO stream?

Stream: System. IO. Stream is an abstract class that provides standard methods to transfer bytes (read, write, etc.) to the source. It is like a wrapper class to transfer bytes. Classes that need to read/write bytes from a particular source must implement the Stream class.

What is a stream and types of streams in C#?

The stream is basically the sequence of bytes passing through the communication path. There are two main streams: the input stream and the output stream. The input stream is used for reading data from file (read operation) and the output stream is used for writing into the file (write operation).

Which of the following file provides a serial stream of input or output?

A character file is a hardware file that reads/writes data in character by character in a file. These files provide a serial stream of input or output and provide direct access to hardware devices.


2 Answers

Use WebClient.OpenRead :

Using wc As New WebClient()
    Using stream As Stream = wc.OpenRead(msgURL)
        ...
    End Using
End Using
like image 93
Thomas Levesque Avatar answered Oct 22 '22 19:10

Thomas Levesque


VB.Net:

Dim req As WebRequest = HttpWebRequest.Create("url here")
Using stream As Stream = req.GetResponse().GetResponseStream()

End Using

C#:

var req = System.Net.WebRequest.Create("url here");
using (Stream stream = req.GetResponse().GetResponseStream())
{

}
like image 28
Joel Coehoorn Avatar answered Oct 22 '22 17:10

Joel Coehoorn