Convert to Stream from a Url
Asked Answered
T

3

31

I was trying to convert an Url to Stream but I am not sure whether I am right or wrong.

protected Stream GetStream(String gazouUrl)
{
    Stream rtn = null;
    HttpWebRequest aRequest = (HttpWebRequest)WebRequest.Create(gazouUrl);
    HttpWebResponse aResponse = (HttpWebResponse)aRequest.GetResponse();

    using (StreamReader sReader = new StreamReader(aResponse.GetResponseStream(), System.Text.Encoding.Default))
    {
        rtn = sReader.BaseStream;
    }
    return rtn;
}

Am I on the right track?

Tombolo answered 24/6, 2010 at 2:56 Comment(1)
Nowadays code should be written this way. I tried a similar thing and got warning "SYSLIB0014: 'WebRequest.Create(string)' is obsolete: 'WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.'"Cyler
A
22

You don't need to create a StreamReader there. Just return aResponse.GetResponseStream();. The caller of that method will also need to call Dispose on the stream when it's done.

Aruspex answered 24/6, 2010 at 3:0 Comment(2)
Please add the code fix here as per requirements for answers.Dervish
For code example see my answer belowPamilapammi
M
47

I ended up doing a smaller version and using WebClient instead the old Http Request code:

private static Stream GetStreamFromUrl(string url)
{
    byte[] imageData = null;

    using (var wc = new System.Net.WebClient())
        imageData = wc.DownloadData(url);

    return new MemoryStream(imageData);
}
Mailable answered 27/9, 2013 at 13:24 Comment(1)
The idea of using a stream is to consume as little memory as possible. With this code, if the file you download from the ul is 2 GB, you will not only have to wait for all of the 2 GB to be downloaded before you can read from the stream, you also consume 2 GB of memory doing so.Longspur
A
22

You don't need to create a StreamReader there. Just return aResponse.GetResponseStream();. The caller of that method will also need to call Dispose on the stream when it's done.

Aruspex answered 24/6, 2010 at 3:0 Comment(2)
Please add the code fix here as per requirements for answers.Dervish
For code example see my answer belowPamilapammi
P
5

The current answer is missing an example in how to use GetResponseStream()

Here is an example

    // Creates an HttpWebRequest with the specified URL.
    HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
    // Sends the HttpWebRequest and waits for the response.         
    HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
    // Gets the stream associated with the response.
    Stream receiveStream = myHttpWebResponse.GetResponseStream();
    Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
    // Pipes the stream to a higher level stream reader with the required encoding format.
    StreamReader readStream = new StreamReader( receiveStream, encode );
Console.WriteLine("\r\nResponse stream received.");
    Char[] read = new Char[256];
    // Reads 256 characters at a time.
    int count = readStream.Read( read, 0, 256 );
    Console.WriteLine("HTML...\r\n");
    while (count > 0)
        {
            // Dumps the 256 characters on a string and displays the string to the console.
            String str = new String(read, 0, count);
            Console.Write(str);
            count = readStream.Read(read, 0, 256);
        }
    Console.WriteLine("");
    // Releases the resources of the response.
    myHttpWebResponse.Close();
    // Releases the resources of the Stream.
    readStream.Close();

For more details see - https://learn.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.getresponsestream?view=net-5.0

Pamilapammi answered 22/6, 2018 at 21:8 Comment(2)
Should be count +=... or else your counter might never reach the length.Longspur
Count is reset for every chunk of data that is read and when is <0 is because the data is finishedPamilapammi

© 2022 - 2024 — McMap. All rights reserved.