Bookmark and Share BitCoin Donate: 13t8gAWVpHP2ddxMp88d1NFpZjnFJC6UwK

Retrieve data from a webpage with c#

using System.Net;
using System.IO;

namespace ashrdotnet{
    public class WebGet{
        static CookieContainer cookieContainer = new CookieContainer();
        private static string getResponse(string url)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
			request.CookieContainer = cookieContainer;
			request.AllowAutoRedirect = true;
			request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET4.0C; .NET4.0E)";

            try{
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    using (StreamReader streamReader = new StreamReader(request.GetResponse().GetResponseStream()))
                    {
                        string data = streamReader.ReadToEnd();
                        return data;
                    }
                }
                else
                {
                    Console.WriteLine("Request for " + request.RequestUri.ToString() + " failed with " + response.StatusCode.ToString());
                }
            }
            catch(Exception e){
                Console.WriteLine(e.Message);
            }

            return null;
        }
    }
}