Return to Snippet

Revision: 52912
at November 4, 2011 20:40 by dkirkland


Initial Code
namespace VK.Snippets
{
    using System.Collections.Generic;
    using System.IO;
    using System.Net;
    using System.Text;

    public static class HttpFetcher
    {
        public static string FetchPage(string url, string username = "", string password = "", string domain = "")
        {
            string page = Encoding.ASCII.GetString(FetchContent(url, username, password, domain));

            return page;
        }

        public static byte[] FetchContent(string url, string username = "", string password = "", string domain = "")
        {
            List<byte> contentBytes = new List<byte>();

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            if (username != "" && password != "" && domain != "")
            {
                request.Credentials = new NetworkCredential(username, password, domain);
            }

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.OK)
            {
                Stream responseStream = response.GetResponseStream();
                
                int b;
                while ((b = responseStream.ReadByte()) != -1)
                {
                    contentBytes.Add((byte)b);
                }
            }

            return contentBytes.ToArray();
        }
    }
}

Initial URL
http://code.google.com/p/vitamink/source/browse/projects/snippets/trunk/src/VK.Snippets/HttpFetcher.cs

Initial Description
Downloads contents as a byte array or string, depending on need.

Initial Title
Download text or bytes via HTTP

Initial Tags
http, download

Initial Language
C#