public static string UrlPathEncode(string s)
        {
#if NET_4_0
            return(HttpEncoderFromMono.Current.UrlPathEncode(s));
#else
            return(HttpEncoderFromMono.UrlPathEncode(s));
#endif
        }
        public static byte [] UrlEncodeToBytes(byte [] bytes, int offset, int count)
        {
            if (bytes == null)
            {
                return(null);
            }
#if NET_4_0
            return(HttpEncoderFromMono.Current.UrlEncode(bytes, offset, count));
#else
            return(HttpEncoderFromMono.UrlEncodeToBytes(bytes, offset, count));
#endif
        }
        public static void HtmlAttributeEncode(string s, TextWriter output)
        {
            if (output == null)
            {
#if NET_4_0
                throw new ArgumentNullException("output");
#else
                throw new NullReferenceException(".NET emulation");
#endif
            }
#if NET_4_0
            HttpEncoderFromMono.Current.HtmlAttributeEncode(s, output);
#else
            output.Write(HttpEncoderFromMono.HtmlAttributeEncode(s));
#endif
        }
        public static string HtmlEncode(string s)
        {
#if NET_4_0
            if (s == null)
            {
                return(null);
            }

            using (var sw = new StringWriter()) {
                HttpEncoderFromMono.Current.HtmlEncode(s, sw);
                return(sw.ToString());
            }
#else
            return(HttpEncoderFromMono.HtmlEncode(s));
#endif
        }
        public static byte [] UrlEncodeUnicodeToBytes(string str)
        {
            if (str == null)
            {
                return(null);
            }

            if (str.Length == 0)
            {
                return(new byte [0]);
            }

            MemoryStream result = new MemoryStream(str.Length);

            foreach (char c in str)
            {
                HttpEncoderFromMono.UrlEncodeChar(c, result, true);
            }
            return(result.ToArray());
        }
        public static string UrlEncode(string s, Encoding Enc)
        {
            if (s == null)
            {
                return(null);
            }

            if (s == String.Empty)
            {
                return(String.Empty);
            }

            bool needEncode = false;
            int  len        = s.Length;

            for (int i = 0; i < len; i++)
            {
                char c = s [i];
                if ((c < '0') || (c < 'A' && c > '9') || (c > 'Z' && c < 'a') || (c > 'z'))
                {
                    if (HttpEncoderFromMono.NotEncoded(c))
                    {
                        continue;
                    }

                    needEncode = true;
                    break;
                }
            }

            if (!needEncode)
            {
                return(s);
            }

            // avoided GetByteCount call
            byte [] bytes   = new byte[Enc.GetMaxByteCount(s.Length)];
            int     realLen = Enc.GetBytes(s, 0, s.Length, bytes, 0);

            return(Encoding.ASCII.GetString(UrlEncodeToBytes(bytes, 0, realLen)));
        }