Geeks With Blogs

News My Blog has been MOVED to https://mfreidge.wordpress.com
Michael Freidgeim's OLD Blog My Blog has been MOVED to https://mfreidge.wordpress.com
Previously I've posted a few Helper Classes . This post describes my HttpRequestHelper class.
      using System;
      using System.Text;
      using System.Web;
      using System.Web.UI;
      using System.Web.UI.WebControls;
 
      ///<summary>
      /// Summary description for HttpRequestHelper.
      ///</summary>
      public class HttpRequestHelper
      {
            // Methods
            public HttpRequestHelper()
            {
            }
            // from http://groups.google.com.au/groups?hl=en&lr=&threadm=oOiDc.121011%24Sw.26011%40attbi_s51&rnum=1
            public static bool IsBrowserOnServer()
            {
                  bool foundIPmatchFlag = false; // Assume no match
                  string userHostAddr= HttpContext.Current.Request.UserHostAddress;
                  string serverhost = Dns.GetHostName(); // get name of server
            IPHostEntry ipserverhost = Dns.GetHostEntry(serverhost);// GetHostByName
                  IPAddress[] addresses = ipserverhost.AddressList; // get list
                  // Look for an IP address match
                  foreach ( IPAddress address in addresses )
                  {
                        if (userHostAddr == address.ToString())
                        {
                              foundIPmatchFlag = true; // got a match
                              break;
                        }
                  }
                  if(foundIPmatchFlag == false) //try more options in case if DNS is not working
                  {
                        if (userHostAddr == "127.0.0.1")
                        {
                              foundIPmatchFlag = true; // got a match
                        }
                        else
                        {
                              string userhostName=HttpContext.Current.Request.UserHostName;
                              userhostName=userhostName.ToLower();
                              if ( (userhostName== "localhost")||(userhostName== serverhost.ToLower()) )
                              {
                                          foundIPmatchFlag = true; // got a match
                              }
                        }
                  }
                  return foundIPmatchFlag;
            }
 
            //created 15/4/2005
            public static string HttpURL(string ServerName, string VirtualDirectory, string ServiceUrl)
            {
            return UriHelper.CombineUrl(HttpBaseUrl(ServerName, VirtualDirectory), ServiceUrl); //8/5/2006 use CombineUrl
            }
        ///<summary>
        /// Returns BaseUrl(e.g. http://Server/VirtualDirectory/" including tailing "/"
        ///</summary>
        ///<param name="ServerName"></param>
        ///<param name="VirtualDirectory"></param>
        ///<returns></returns>
            public static string HttpBaseUrl(string ServerName, string VirtualDirectory)
            {
                  string sBaseUrl=string.Format("{0}/{1}/", ServerName,VirtualDirectory);
                  if(false==sBaseUrl.ToLower().StartsWith("http://"))
                  {
                        sBaseUrl="http://"+sBaseUrl;
                  }
                  return sBaseUrl;
            }
//from http://www.thescripts.com/forum/thread344270.html
        ///<summary>
        /// consider invalid URI as null
        ///</summary>
        ///<param name="Request"></param>
        ///<returns></returns>
        public static Uri SafeUrlReferrer(HttpRequest Request)
        {
            Uri uriReferrer = null;
            try
            {
                uriReferrer = Request.UrlReferrer;
            }
            catch (UriFormatException exc)//ignore errors Invalid URI: The hostname could not be parsed
            {
                //consider invalid as null,            //ignore in production
                Debug.Assert(false, "just debug when it happens" + exc.ToString());
            }
            return uriReferrer;
        }
        public static string GetReferrerHost(HttpRequest Request)
        {
            Uri uriReferrer = SafeUrlReferrer(Request);
            string referrerHost = null;
            if (uriReferrer != null)
            {
                referrerHost = uriReferrer.Host.ToUpper();
            }
            else
            {
                referrerHost = string.Empty;
            }
            return referrerHost;
        }
 
      }
 
 
Posted on Monday, October 8, 2007 1:06 AM | Back to top


Comments on this post: My HttpRequestHelper class

# re: My HttpRequestHelper class
Requesting Gravatar...
Hey, that was interesting,

excelent code , I love how you wrote this articles

Thanks for writing about it
Left by web development company on Aug 27, 2009 12:22 PM

Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net