My HttpRequestHelper class

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 @ Monday, October 08, 2007 1:06 AM

Print

Comments on this entry:

No comments posted yet.

Your comment:



 (will not be displayed)


 
 
 
Please add 2 and 6 and type the answer here:
 

Live Comment Preview:

 
«December»
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910