程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#自界說針對URL地址的處置類實例

C#自界說針對URL地址的處置類實例

編輯:C#入門知識

C#自界說針對URL地址的處置類實例。本站提示廣大學習愛好者:(C#自界說針對URL地址的處置類實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#自界說針對URL地址的處置類實例正文


本文實例講述了C#自界說針對URL地址的處置類。分享給年夜家供年夜家參考。詳細剖析以下:

這個C#類是專門針對URL網址處置的類,可以對URL地址停止Base64的加密息爭密,可以經由過程函數的方法向URL添加參數,可以更新URL中已有參數的值,剖析URL地址的域名、子域名,剖析URL的一切參數和參數值,功效異常周全,還可以本身擴大,異常具有適用價值

using System;
using System.Text.RegularExpressions;
using System.Web;
using System.Collections.Specialized;
namespace DotNet.Utilities
{
  /// <summary>
  /// URL的操作類
  /// </summary>
  public class UrlOper
  {
    static System.Text.Encoding encoding = System.Text.Encoding.UTF8;
    #region URL的64位編碼
    public static string Base64Encrypt(string sourthUrl)
    {
      string eurl = HttpUtility.UrlEncode(sourthUrl);
      eurl = Convert.ToBase64String(encoding.GetBytes(eurl));
      return eurl;
    }
    #endregion
    #region URL的64位解碼
    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>
    /// 能否是Base64字符串
    /// </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>
    /// 添加URL參數
    /// </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>
    /// 更新URL參數
    /// </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 剖析URL所屬的域
    public static void GetDomain(string fromUrl, out string domain, out string subDomain)
    {
      domain = "";
      subDomain = "";
      try
      {
        if (fromUrl.IndexOf("的咭片") > -1)
        {
          subDomain = fromUrl;
          domain = "咭片";
          return;
        }
        UriBuilder builder = new UriBuilder(fromUrl);
        fromUrl = builder.ToString();
        Uri u = new Uri(fromUrl);
        if (u.IsWellFormedOriginalString())
        {
          if (u.IsFile)
          {
            subDomain = domain = "客戶端當地文件途徑";
          }
          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).WordStr("comhttp","com");
            subDomain = Authority.WordStr("comhttp", "com");
            if (ss.Length < 2)
            {
              domain = "不明途徑";
              subDomain = "不明途徑";
            }
          }
        }
        else
        {
          if (u.IsFile)
          {
            subDomain = domain = "客戶端當地文件途徑";
          }
          else
          {
            subDomain = domain = "不明途徑";
          }
        }
      }
      catch
      {
        subDomain = domain = "不明途徑";
      }
    }
    /// <summary>
    /// 剖析 url 字符串中的參數信息。
    /// </summary>
    /// <param name="url">輸出的 URL</param>
    /// <param name="baseUrl">輸入 URL 的基本部門</param>
    /// <param name="nvc">輸入剖析後獲得的 (參數名,參數值) 的聚集</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);
      // 開端剖析參數對  
      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
  }
}

願望本文所述對年夜家的C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved