C # Custom processing class for URL addresses

Source: Internet
Author: User
Tags urlencode subdomain

Specifically for the URL address processing of the class, the URL can be Base64 encryption and decryption, 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, can also expand their own, sharejs.com recommended, very practical value

usingSystem;usingSystem.Text.RegularExpressions;usingsystem.web;usingSystem.Collections.Specialized;namespacedotnet.utilities{/// <summary>///action classes for URLs/// </summary> Public classurloper{StaticSystem.Text.Encoding Encoding =System.Text.Encoding.UTF8;#region64-bit encoding of URLs Public Static stringBase64encrypt (stringSourthurl) {stringEurl =Httputility.urlencode (sourthurl); Eurl=convert.tobase64string (encoding. GetBytes (Eurl));returnEurl;}#endregion#region64-bit decoding of URLs Public Static stringBase64decrypt (stringestr) {if(!IsBase64 (ESTR)) {returnEstr;}byte[] buffer =convert.frombase64string (ESTR);stringSourthurl =encoding. GetString (buffer); Sourthurl=Httputility.urldecode (sourthurl);returnSourthurl;}/// <summary>///whether it is a Base64 string/// </summary>/// <param name= "Estr" ></param>/// <returns></returns> Public Static BOOLIsBase64 (stringestr) {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 stringAddParam (stringUrlstringParamName,stringvalue) {URI Uri=NewUri (URL);if(string. IsNullOrEmpty (URI. Query)) {stringeval =HttpContext.Current.Server.UrlEncode (value);returnString.Concat (URL,"?"+ ParamName +"="+eval);}Else{stringeval =HttpContext.Current.Server.UrlEncode (value);returnString.Concat (URL,"&"+ ParamName +"="+eval);}}/// <summary>///Update URL Parameters/// </summary> Public Static stringUpdateparam (stringUrlstringParamName,stringvalue) {stringKeyWord = paramname+"=";intindex = URL. IndexOf (KeyWord) +keyword.length;intindex1 = URL. IndexOf ("&", index);if(Index1 = =-1) {URL= URL. Remove (index, URL. Length-index); URL=string. Concat (URL, value);returnURL;} URL= URL. Remove (Index,index1-index); URL=URL. Insert (index, value);returnURL;}#regionAnalyze the domain to which the URL belongs Public Static voidGetDomain (stringFromurl, out stringDomain out stringsubdomain) {Domain=""; subdomain="";Try{if(Fromurl.indexof ("'s business card") >-1) {subdomain=Fromurl;domain="Business Card";return;} UriBuilder Builder=NewUriBuilder (fromurl); Fromurl=Builder. ToString (); Uri u=NewUri (fromurl);if(U.iswellformedoriginalstring ()) {if(u.isfile) {subdomain= Domain ="client local file path";}Else{stringauthority =u.authority;string[] ss = U.authority.split ('.');if(SS. Length = =2) {Authority="www."+Authority;}intindex = Authority.indexof ('.',0);d Omain= 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>///parses the parameter information in the URL string. Sharejs.com/// </summary>/// <param name= "url" >the URL entered</param>/// <param name= "BaseUrl" >the underlying part of the output URL</param>/// <param name= "NVC" >a collection of (parameter names, parameter values) obtained after output analysis</param> Public Static voidparseURL (stringUrl out stringBASEURL, outNameValueCollection NVC) {if(url = =NULL)Throw NewArgumentNullException ("URL"); NVC=NewNameValueCollection (); BaseUrl="";if(url = ="")return;intQuestionmarkindex = URL. IndexOf ('?');if(Questionmarkindex = =-1) {BaseUrl=URL;return;} BASEURL= URL. Substring (0, Questionmarkindex);if(questionmarkindex = = URL.) Length-1)return;stringps = URL. Substring (Questionmarkindex +1);//Start analyzing parameter pairsRegex re =NewRegex (@"(^|&)? (\w+) = ([^&]+) (&|$)?", regexoptions.compiled); MatchCollection MC=Re. Matches (PS);foreach(Match minchMC) {NVC. ADD (M.result (" $"). ToLower (), M.result (" $"));}}#endregion}}

C # Custom processing class 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.