.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Code Snippets >> Windows Form Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Getting website content using url

Posted By:Jean Paul       Posted Date: March 26, 2011    Points: 40    Category: Windows Form Controls    URL: http://www.dotnetspark.com  

Returns the website content as string for given website url

public string GetHttpResponse(string url)
                ASCIIEncoding encoding = new ASCIIEncoding();

                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
                myRequest.Method = "GET";

                HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse();

                return GetResponseContent(response);
            catch (Exception ex)

            return String.Empty;

        /// Returns the string content of HttpWebResponse
        private string GetResponseContent(HttpWebResponse response)
            if (response == null)
                return String.Empty;

            StringBuilder builder = new StringBuilder();
            Stream stream = response.GetResponseStream();

            StreamReader streamReader = new StreamReader(stream);

            int data = 0;
                data = streamReader.Read();
                if (data > -1)
            while (data > -1);


            return builder.ToString();


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend