using System;
using System.Net;
using System.IO;
using System.Text;

// Gets a URL (http or https) and creates a stream.
// Has utility design pattern, with no instance allowed.
public class WebStreamUtil
{
    private WebStreamUtil() {} // No instances allowed

    public static Stream Create(string url)
    {
       return Create(url, "utf-8");
    }
    public static Stream Create(string url, string encod)
    {
        WebRequest req = WebRequest.Create(url);
        WebResponse result = req.GetResponse();
        Stream ReceiveStream = result.GetResponseStream();
        Encoding enc = System.Text.Encoding.GetEncoding(encod); 

        StreamReader sr = new StreamReader( ReceiveStream, enc);
        Char[] cbuf = new Char[256];
        int len;
        StringBuilder build = new StringBuilder();
        while((len = sr.Read(cbuf, 0, cbuf.Length)) > 0) 
        {
           string tmp = new string(cbuf, 0, len);
           build.Append(tmp);
        }
        sr.Close();

        byte[] byt = (new ASCIIEncoding()).GetBytes(build.ToString());

        return new MemoryStream(byt);
    }

    public static int MaxContentLength = 16384; // 16k
}