Correctly encode a name for a URL. : URI « Network « C# / C Sharp






Correctly encode a name for a URL.

      

//CruiseControl is open source software and is developed and maintained by a group of dedicated volunteers. 
//CruiseControl is distributed under a BSD-style license.
//http://cruisecontrol.sourceforge.net/
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace ThoughtWorks.CruiseControl.Core.Util
{
    /// <summary>
    /// Class with handy stirng routines
    /// </summary>
    public class StringUtil
    {
        private static readonly Regex urlEncodeRegex = new Regex("[^a-zA-Z0-9\\.\\-_~]", RegexOptions.Compiled);

        // public for testing only
        public const string DEFAULT_DELIMITER = ",";


        /// <summary>
        /// Correctly encode a name for a URL.
        /// </summary>
        /// <param name="name">The name to encode.</param>
        /// <returns>The encoded name.</returns>
        /// <remarks>
        /// <para>
        /// HttpUtility.UrlEncode does not correctly encode for a URL, spaces get converted into 
        /// pluses, which can cause security errors.
        /// </para>
        /// <para>
        /// This method will encode characters according to RFC 3986. This means only the following 
        /// characters are allowed un-encoded:
        /// </para>
        /// <para>
        /// A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s 
        /// t u v w x y z 0 1 2 3 4 5 6 7 8 9 - _ . ~
        /// </para>
        /// <para>
        /// However, since the encoding only uses two-hex digits, it is not possible to encode non-ASCII
        /// characters using this approach. Therefore we are using the RFC 3986 recommendation and assuming
        /// the string will be using UTF-8 encoding and leaving the characters as they are.
        /// </para>
        /// </remarks>
        public static string UrlEncodeName(string name)
        {
            var encodedName = urlEncodeRegex.Replace(name, (match) =>
            {
                var charValue = (int)match.Value[0];
                var value = charValue >= 255 ? match.Value : "%" + string.Format("{0:x2}", charValue);
                return value;
            });
            return encodedName;
        }
    }
}

   
    
    
    
    
    
  








Related examples in the same category

1.Use UriUse Uri
2.Build the hash table of HTML entity references and encode Url
3.Is Relative Url
4.Is Rooted Url
5.Essentially creates a query string.
6.Uri Class Provides an object representation of a uniform resource identifier (URI) and easy access to the parts of the URI.
7.Initializes a new instance of the Uri class with the specified URI.
8.Initializes a new instance of the Uri class based on the specified base URI and relative URI string.
9.Gets the absolute path of the URI.
10.Gets the absolute URI.
11.Gets the Domain Name System (DNS) host name or IP address and the port number for a server.
12.Determines whether the specified host name is a valid DNS name.
13.Determines whether the specified scheme name is valid.
14.Gets an unescaped host name that is safe to use for DNS resolution.
15.Gets the escaped URI fragment.
16.Gets the decimal value of a hexadecimal digit.
17.Gets the hash code for the URI.
18.Gets the specified portion of a Uri instance.
19.Converts a specified character into its hexadecimal equivalent.
20.Gets the host component of this instance.
21.Gets the type of the host name specified in the URI.
22.Gets whether the port value of the URI is the default for this scheme.
23.Gets a value indicating whether the specified Uri is a file URI.
24.Determines whether a specified character is a valid hexadecimal digit.
25.Gets whether the specified Uri references the local host.
26.Gets whether the specified Uri is a universal naming convention (UNC) path.
27.Gets a local operating-system representation of a file name.
28.Determines the difference between two Uri instances.
29.Gets the original URI string that was passed to the Uri constructor.
30.Gets the AbsolutePath and Query properties separated by a question mark (?).
31.Gets the port number of this URI.
32.Gets any query information included in the specified URI.
33.Gets the scheme name for this URI.
34.Gets an array containing the path segments that make up the specified URI.
35.Gets a canonical string representation for the specified Uri instance.
36.Specifies that the URI is a pointer to a file.
37.Specifies that the URI is accessed through the File Transfer Protocol (FTP).
38.Specifies that the URI is accessed through the Gopher protocol.
39.Specifies that the URI is accessed through the Hypertext Transfer Protocol (HTTP).
40.Specifies that the URI is accessed through the Secure Hypertext Transfer Protocol (HTTPS).
41.Specifies that the URI is an e-mail address and is accessed through the Simple Mail Transport Protocol (SMTP).
42.Specifies that the URI is an Internet news group and is accessed through the Network News Transport Protocol (NNTP).
43.Uri.UriSchemeNntp
44.Indicates that the URI string was completely escaped before the Uri instance was created.
45.Gets the user name, password, or other user-specific information associated with the specified URI.
46.Download text data from the specified URI
47.Expand Uri
48.Expand Relative Uri
49.Parse the query string in the URI into a KeyValuePair
50.Assembles a series of key=value pairs as a URI-escaped query-string.
51.Get Absolute Url For Local File
52.Get Post ID From URL
53.Combine URL
54.Url Encode
55.URL Encoding
56.Url encoding (2)
57.Url Encode 2
58.Url Encode 3
59.Retrieves the subdomain from the specified URL.
60.Try to parse the url, similar to int.TryParse
61.Is Link Valid