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


Top 5 Contributors of the Month
david stephan

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)
        {
            try
            {
                ASCIIEncoding encoding = new ASCIIEncoding();

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

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

                return GetResponseContent(response);
            }
            catch (Exception ex)
            {
                HandleException(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;
            do
            {
                data = streamReader.Read();
                if (data > -1)
                    builder.Append((char)data);
            }
            while (data > -1);

            streamReader.Close();

            return builder.ToString();
        }

     

Further Readings:

Responses

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