C # Customizing Processing class instances for URL addresses

Source: Internet
Author: User
Tags subdomain

The examples in this article describe C # Custom processing classes for URL addresses. Share to everyone for your reference. The specific analysis is as follows:

This C # class is specifically for the URL address processing of the class, you can Base64 the URL addresses to encrypt and decrypt, you can add parameters to the URL by function, you can update the URL of the parameters of the value of the URL address, the domain name, sub-domain name, analysis URL of all parameters and parameter values, The function is very comprehensive, also can expand oneself, very has the practical value

Using system;using system.text.regularexpressions;using system.web;using system.collections.specialized;namespace dotnet.utilities{///<summary>///////</summary> public class Urloper {static SYSTEM.TEXT.E    ncoding encoding = System.Text.Encoding.UTF8; The 64-bit encoding of the #region URL public static string Base64encrypt (String sourthurl) {string eurl = Httputility.urlencode (so      Urthurl); Eurl = convert.tobase64string (encoding.      GetBytes (Eurl));    return eurl; The 64-bit decoding public static string Base64decrypt (String estr) {if (!) #endregion #region URL.      IsBase64 (ESTR)) {return estr;      } byte[] buffer = convert.frombase64string (ESTR); String sourthurl = Encoding.      GetString (buffer);      Sourthurl = Httputility.urldecode (Sourthurl);    return sourthurl;  }//<summary>//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, Strin      G 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 Parse URL that belongs to the domain 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> parse the parameter information in the URL string. </summary>//<param name= "url" > Input url</param>//<param name= "baseUrl" > Output URL Base part & lt;/param>//<param name= "NVC" > Output analysis (parameter name, parameter value) collection </param> public static Void parseurl (string url, out string baseUrl, out NameValueCollection NVC) {if (url = = null) throw new Argume      Ntnullexception ("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 parsing parameters to regex re = new regex (@ "(^|&)?" (      \w+) = ([^&]+) (&|$)? ", regexoptions.compiled); MatchCollection mc = Re.      Matches (PS); foreach (Match m in MC) {NVC. ADD (M.result ("$").      ToLower (), M.result ("$")); }} #endregion}}

I hope this article is helpful to everyone's C # programming.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
C # Customizing Processing class instances for URL addresses

This address: http://www.paobuke.com/develop/c-develop/pbk23057.html






Related content Add SQLite transaction operations support and Usage methods in C # design Pattern Series Tutorial-state mode C # function overlay Learning (recommended) on C # pointer problems
Summary of 5 C # string manipulation methods share C # to determine whether the specified partition is in NTFS format unity implements multi-touch code understanding events in C #

C # Customizing Processing class instances for URL addresses

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.