Request in c#; how to get a web page content fast

This is a start-up information, when you try to made an program that should us the Internet. Of course, many domains are already covered, example using the web services, but you can still find places where to use separat web requests. What happens if you need to grab a image from the Internet, over http? Or a web page?

 

This problem is solved by this method:

       static Stream GetStream(string URL)
        {
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);



            // Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myRequest.GetResponse();

            #region Encoding
            Encoding encode = Encoding.UTF8;
            try
            {
                encode = System.Text.Encoding.GetEncoding(myHttpWebResponse.CharacterSet);
            }
            catch { }
            #endregion


            Stream streamResponse = myHttpWebResponse.GetResponseStream();


            //myHttpWebResponse.Close();
            return streamResponse;//Return the page as an Stream
        }

 

It works, but sometime you will need cookies, or you have to use a proxy server or many thinck like this. A long time ago, I create a class for working with requests. It can be downloaded by here; the ideea is when you have to use the same code again and again, it has to be optimized. In this moment my code is not very optimized, I just do it and forget about it, probably because it works. In the future this will be a project for an CodeProject article codeproject.com, where a long time ago, I promise to my self to put more code there :) .

coninue


Posted by: admin
Posted on: 9/21/2009 at 12:29 AM
Tags: , , ,
Categories: .NET | ASP | Programing
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed