Mar 4, 2008

Reading URL thru HttpRequest in C#

Here is the sample code for reading Http request.

 

HttpWebRequest request = null;

   string url = “http://matespoint.blogspot.com”

   // Create web request

   request = (HttpWebRequest)WebRequest.Create(url);

   // Set value for request headers

   request.Method = "GET";

   request.ProtocolVersion = HttpVersion.Version11;

   request.AllowAutoRedirect = false;

   request.Accept = "*/*";

   request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)";

   request.Headers.Add("Accept-Language", "en-us");

   request.KeepAlive = true;

 

   StreamReader responseStream = null;

   HttpWebResponse webResponse = null;

   string webResponseStream = string.Empty;

   // Get response for http web request

   webResponse = (HttpWebResponse)request.GetResponse();

   responseStream = new StreamReader(webResponse.GetResponseStream());

 

// Read web response into string

   webResponseStream = responseStream.ReadToEnd();