An introduction to System.Web.HttpWebRequest

The Microsoft.NET framework gives developers the ability to send out WebRequests to other sites, to retrieve their content and process it (its quite useful for PayPal integration), and it is fairly simple, using the HttpWebRequest class built into the framework.

Apart from sending the request, you will need something to process the response, typically this will be the HttpWebResponse, although you can build your own custom response processor. HttpWebResponse will provide you with additional information such as the Headers sent by the page and the status code returned.

Here is actual production code that I use to send out requests, and receive a string response from a page (do note, this will not be much use if you are trying to save / download images or other binary data)

Just a small note - I spoof the user agent, since quite a few sites dont like it when you access them with the .net framework.

Heres one I prepared earlier

public  static  string GetPageResponse(string URL)
        {
            return GetPageResponse(URL, URL);
        }
 
        public static string GetPageResponse(string URL, string Referer)
        {
            string UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)";
            return GetPageResponse(Referer, URL, UserAgent);
        }
 
        public static string GetPageResponse(string Referer, string URL, string UserAgent)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(URL);
            webRequest.KeepAlive = false;
            webRequest.Referer = Referer;
            webRequest.UserAgent = UserAgent;
            HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;
            StreamReader reader = new StreamReader(webResponse.GetResponseStream());
            return reader.ReadToEnd();
        }