Sometimes you need to read a remote ( or local ) web page for scraping the content or ajax save, etc.
Async version has been tested by calling it 10000 times, and watching the threads for leaks - no problems found. This is good when you want to do a lengthy process in the background, when the user is looking at the thanks page.
Here are a couple of functions:
/// <summary> /// wraps up the .NET HttpWebRequest and HttpWebResponse to make a page request simpler /// </summary> /// <param name="URL">the URL to request (can be relative or absolute)</param> /// <param name="ErrorFormat">the string to return if there is an error. "URL" will be matched and replaced with the requested URL if it is found</param> /// <returns>the response as a string</returns> public static string HttpGet(string URL, string ErrorFormat) { return HttpGet(URL,ErrorFormat,Web.Request); } /// <summary> /// make an async call to a url, and don't return any result /// </summary> /// <param name="URL"></param> public static void HttpGetAsync(string URL) { Util.HttpGetAsync(URL,"HttpGet ERROR requesting: URL"); } /// <summary> /// make an async call to a url, and don't return any result /// </summary> /// <param name="URL"></param> /// <param name="ErrorFormat"></param> public static void HttpGetAsync(string URL, string ErrorFormat) { //create the delegate HttpGetAsyncDelegate asyncAction = HttpGetAsyncAction; //invoke it asynchrnously, control passes to next statement asyncAction.BeginInvoke(URL,ErrorFormat,Web.Request,null,null); } private delegate void HttpGetAsyncDelegate(string URL, string ErrorFormat, HttpRequest req); //delegate for the action private static void HttpGetAsyncAction(string URL, string ErrorFormat, HttpRequest req) { Util.HttpGet(URL,ErrorFormat??"HttpGet ERROR requesting: URL",req); } /// <summary> /// wraps up the .NET HttpWebRequest and HttpWebResponse to make a page request simpler /// </summary> /// <param name="URL">the URL to request (can be relative or absolute)</param> /// <param name="ErrorFormat">the string to return if there is an error. "URL" will be matched and replaced with the requested URL if it is found</param> /// <returns>the response as a string</returns> public static string HttpGet(string URL, string ErrorFormat, HttpRequest req) { string returnValue = ""; // if no "http://" in URL then relative URL was meant if(!URL.StartsWith("http://")) { URL = URL.TrimStart(new Char[] {'/'}); // trim off a leading slash if there is one Uri currentUri = req.Url; //TODO: make the next line a bit more robust?? string virtualPath = (ServerIs() != "LVE") ? currentUri.Segments[1] : ""; // this assumes dev and staging both use a virtual path URL = String.Format("http://{0}{1}{2}" + URL, currentUri.Host, currentUri.Segments[0], virtualPath ); } HttpWebRequest httpRequest = WebRequest.Create(URL) as HttpWebRequest; if (httpRequest != null) { httpRequest.Method = "GET"; // MN 20100725 - experiment //if (async) { // var asyncResponse = httpRequest.BeginGetResponse(null, null); // httpRequest.EndGetResponse(asyncResponse); //} try { HttpWebResponse httpResponse = httpRequest.GetResponse() as HttpWebResponse; if (httpResponse != null) { StreamReader sr = new StreamReader(httpResponse.GetResponseStream()); returnValue = sr.ReadToEnd(); sr.Close(); httpResponse.Close(); } } catch { // try and replace the URL if we find it - if not, just return the user's Error String returnValue = ErrorFormat.Replace("URL", URL); } } return returnValue; } #endregion
Leave a Comment
Comments