. Net uses urlencode in winform for string encoding and uppercase letters

Source: Internet
Author: User

. Net urlencode encoded strings are all lowercase letters. to use this method in winform, do not reference redundant Assembly, such as system. web and so on. Therefore, this method is extracted and slightly modified. In this way, all the letters in the encoded string are in uppercase. See the following ~ Share it .. The efficiency should be faster than that spread outside.

 

public static string UrlEncode( string str ) {            return UrlEncode( str, System.Text.Encoding.UTF8 );        }        public static string UrlEncode( string str, Encoding e ) {            if (str == null) {                return null;            }            return Encoding.ASCII.GetString( UrlEncodeToBytes( str, e ) );        }        private static byte[] UrlEncodeToBytes( string str, Encoding e ) {            if (str == null) {                return null;            }            byte[] bytes = e.GetBytes( str );            return UrlEncodeBytesToBytesInternal( bytes, 0, bytes.Length, false );        }        private static byte[] UrlEncodeBytesToBytesInternal( byte[] bytes, int offset, int count, bool alwaysCreateReturnValue ) {            int num = 0;            int num2 = 0;            for (int i = 0; i < count; i++) {                char ch = (char)bytes[offset + i];                if (ch == ' ') {                    num++;                }                else if (!IsSafe( ch )) {                    num2++;                }            }            if ((!alwaysCreateReturnValue && (num == 0)) && (num2 == 0)) {                return bytes;            }            byte[] buffer = new byte[count + (num2 * 2)];            int num4 = 0;            for (int j = 0; j < count; j++) {                byte num6 = bytes[offset + j];                char ch2 = (char)num6;                if (IsSafe( ch2 )) {                    buffer[num4++] = num6;                }                else if (ch2 == ' ') {                    buffer[num4++] = 43;                }                else {                    buffer[num4++] = 37;                    buffer[num4++] = (byte)IntToHex( (num6 >> 4) & 15 );                    buffer[num4++] = (byte)IntToHex( num6 & 15 );                }            }            return buffer;        }        private static bool IsSafe( char ch ) {            if ((((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z'))) || ((ch >= '0') && (ch <= '9'))) {                return true;            }            switch (ch) {                case '\'':                case '(':                case ')':                case '*':                case '-':                case '.':                case '_':                case '!':                    return true;            }            return false;        }        private static char IntToHex( int n ) {            if (n <= 9) {                return (char)(n + 48);            }            return (char)((n - 10) + 65);        }

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.