C customizes handling class instances for URL addresses

  • 2021-01-22 05:19:26
  • OfStack

This article illustrates C#'s custom handling classes for URL addresses. Share with you for your reference. The specific analysis is as follows:

The C # class is designed for URL url processing class, can to Base64 URL address of encryption and decryption, you can add a parameter to URL by means of function, can have the value of the parameter, updating URL analysis domain and subdomain URL address, analysis URL all parameters and parameter values, function is very comprehensive, can also be expanded, has very practical value


using System;
using System.Text.RegularExpressions;
using System.Web;
using System.Collections.Specialized;
namespace DotNet.Utilities
{
  /// <summary>
  /// URL The operation of the class 
  /// </summary>
  public class UrlOper
  {
    static System.Text.Encoding encoding = System.Text.Encoding.UTF8;
    #region URL the 64 A coding 
    public static string Base64Encrypt(string sourthUrl)
    {
      string eurl = HttpUtility.UrlEncode(sourthUrl);
      eurl = Convert.ToBase64String(encoding.GetBytes(eurl));
      return eurl;
    }
    #endregion
    #region URL the 64 A decoding 
    public static string Base64Decrypt(string eStr)
    {    
      if (!IsBase64(eStr))
      {
        return eStr;
      }
      byte[] buffer = Convert.FromBase64String(eStr);
      string sourthUrl = encoding.GetString(buffer);
      sourthUrl = HttpUtility.UrlDecode(sourthUrl);
      return sourthUrl;
    }
    /// <summary>
    ///  Whether it is Base64 string 
    /// </summary>
    /// <param name="eStr"></param>
    /// <returns></returns>
    public static bool IsBase64(string eStr)
    {
      if ((eStr.Length % 4) != 0)
      {
        return false;
      }
      if (!Regex.IsMatch(eStr, "^[A-Z0-9/+=]*$", RegexOptions.IgnoreCase))
      {
        return false;
      }
      return true;
    }
    #endregion
    /// <summary>
    ///  add URL parameter 
    /// </summary>
    public static string AddParam(string url, string paramName, string value)
    {
      Uri uri = new Uri(url);
      if (string.IsNullOrEmpty(uri.Query))
      {
        string eval = HttpContext.Current.Server.UrlEncode(value);
        return String.Concat(url, "?" + paramName + "=" + eval);
      }
      else
      {
        string eval = HttpContext.Current.Server.UrlEncode(value);
        return String.Concat(url, "&" + paramName + "=" + eval);
      }
    }
    /// <summary>
    ///  update URL parameter 
    /// </summary>
    public static string UpdateParam(string url, string paramName, string value)
    {
      string keyWord = paramName+"=";
      int index = url.IndexOf(keyWord)+keyWord.Length;
      int index1 = url.IndexOf("&", index);
      if (index1 == -1)
      {
        url = url.Remove(index, url.Length - index);
        url = string.Concat(url, value);
        return url;
      }
      url = url.Remove(index,index1 - index);
      url = url.Insert(index, value);
      return url;
    }
    #region  Analysis of the URL Belongs to the domain of 
    public static void GetDomain(string fromUrl, out string domain, out string subDomain)
    {
      domain = "";
      subDomain = "";
      try
      {
        if (fromUrl.IndexOf(" Business card ") > -1)
        {
          subDomain = fromUrl;
          domain = " Business card ";
          return;
        }
        UriBuilder builder = new UriBuilder(fromUrl);
        fromUrl = builder.ToString();
        Uri u = new Uri(fromUrl);
        if (u.IsWellFormedOriginalString())
        {
          if (u.IsFile)
          {
            subDomain = domain = " Client local file path ";
          }
          else
          {
            string Authority = u.Authority;
            string[] ss = u.Authority.Split('.');
            if (ss.Length == 2)
            {
              Authority = "www." + Authority;
            }
            int index = Authority.IndexOf('.', 0);
            domain = Authority.Substring(index + 1, Authority.Length - index - 1).Replace("comhttp","com");
            subDomain = Authority.Replace("comhttp", "com");
            if (ss.Length < 2)
            {
              domain = " Unknown path ";
              subDomain = " Unknown path ";
            }
          }
        }
        else
        {
          if (u.IsFile)
          {
            subDomain = domain = " Client local file path ";
          }
          else
          {
            subDomain = domain = " Unknown path ";
          }
        }
      }
      catch
      {
        subDomain = domain = " Unknown path ";
      }
    }
    /// <summary>
    ///  Analysis of the  url  Parameter information in the string. 
    /// </summary>
    /// <param name="url"> The input of  URL</param>
    /// <param name="baseUrl"> The output  URL  The foundation of </param>
    /// <param name="nvc"> After the output analysis  ( Parameter names , The parameter value )  A collection of </param>
    public static void ParseUrl(string url, out string baseUrl, out NameValueCollection nvc)
    {
      if (url == null)
        throw new ArgumentNullException("url");
      nvc = new NameValueCollection();
      baseUrl = "";
      if (url == "")
        return;
      int questionMarkIndex = url.IndexOf('?');
      if (questionMarkIndex == -1)
      {
        baseUrl = url;
        return;
      }
      baseUrl = url.Substring(0, questionMarkIndex);
      if (questionMarkIndex == url.Length - 1)
        return;
      string ps = url.Substring(questionMarkIndex + 1);
      //  Start analyzing parameter pairs   
      Regex re = new Regex(@"(^|&)?(\w+)=([^&]+)(&|$)?", RegexOptions.Compiled);
      MatchCollection mc = re.Matches(ps);
      foreach (Match m in mc)
      {
        nvc.Add(m.Result("$2").ToLower(), m.Result("$3"));
      }
    }
    #endregion
  }
}

I hope this article will be helpful to your C# programming.


Related articles: