C# 实现 java URLEncoder.encode编码

最近碰到一个很坑很难受的问题,公司对接一个java项目碰到C# url编码跟java url编码后不一样,导致最后加密结果不一样,** C# 中文url编码后是小写的 java 中文url编码后是大写的,且会对非安全字符进行编码


这里得注意一下,有些地方个别情况特殊符号没进行编码的,还是得自己手动Replace一下对应编码

 public static class Utils
    {
       private static string UrlEncode(this string str)
        {
            var encoding = UTF8Encoding.UTF8;
            byte[] bytes = encoding.GetBytes(str);
            int IsSafe = 0;
            int NoSafe = 0;
            for (int i = 0; i < bytes.Length; i++)
            {
                char ch = (char)bytes[i];
                if (ch == ' ')
                {
                    IsSafe++;
                }
                else if (!IsSafeChar(ch))
                {
                    NoSafe ++;
                }
            }
            if (IsSafe== 0 && NoSafe == 0)
            {
                return str;
            }
            byte[] buffer = new byte[bytes.Length + (NoSafe  * 2)];
            int num1 = 0;
            for (int j = 0; j < bytes.Length; j++)
            {
                byte num2 = bytes[j];
                char ch2 = (char)num2;
                if (IsSafeChar(ch2))
                {
                    buffer[num1++] = num2;
                }
                else if (ch2 == ' ')
                {
                    buffer[num1++] = 0x2B;
                }
                else
                {
                    buffer[num1++] = 0x25;
                    buffer[num1++] = (byte)IntToHex((num2 >> 4) & 15);
                    buffer[num1++] = (byte)IntToHex(num2 & 15);
                }
            }
            return encoding.GetString(buffer);
        }

       private static bool IsSafeChar(char ch)
        {
            if ((((ch < 'a') || (ch > 'z')) && ((ch < 'A') || (ch > 'Z'))) && ((ch < '0') || (ch > '9')))
            {

                switch (ch)
                {
                    case '-':
                    case '.':
                        break;  //安全字符
                    case '+':
                    case ',':
                        return false;  //非安全字符
                    default:   //非安全字符
                        if (ch != '_')
                        {
                            return false;
                        }
                        break;
                }
            }
            return true;
        }
        private static char IntToHex(int n)
        {
            if (n <= 9)
            {
                return (char)(n + 0x30);  
            }
            return (char)((n - 10) + 0x41);
        }
    }

博文最后更新时间:


评论

  • smcoaches

    <a href="http://www.docinsider.net/nike-mercurial-superfly-blackout-fg-for-cheap-billig-obuvuk">nike mercurial superfly blackout fg for cheap billig</a> <a href="http://www.fotowerft.net/atlanta-braves-veterans-day-hat-ru-capd">atlanta braves veterans day hat ru</a> <a href="http://www.brassmedia.net/nike-air-max-95-size-4-uk-billig-booto">nike air max 95 size 4 uk billig</a> <a href="http://www.sagerllp.net/neymar-in-brazil-jersey-nfle">neymar in brazil jersey</a> <a href="http://www.gastropol.net/cheap-all-gray-nike-shox-turbo-2-skoit">cheap all gray nike shox turbo 2</a> <a href="http://www.sweetpee.net/adidas-nmd-r2-ash-pearl-billig-zapatoso">adidas nmd r2 ash pearl billig</a> smcoaches http://www.smcoaches.com/

  • fracingsand

    <a href="https://www.transportaweb.com/cases-for-tcl-casec">cases for tcl</a> <a href="https://www.btmkinjury.com/taxi-portemonnee-tassenb">taxi portemonnee</a> <a href="https://www.allezredmond.com/%C4%8Dern%C3%A1-iphone-clear-pouzdro-pouzdroe">膷ern谩 iphone clear pouzdro</a> <a href="https://www.jasonlinphoto.com/long-sleeve-button-through-%C5%A1aty-satyc">long sleeve button through 拧aty</a> <a href="https://www.rpopartner.com/texas-rangers-bianca-batting-practice-39thirty-berretto-cappelloa">texas rangers bianca batting practice 39thirty berretto</a> <a href="https://www.snorkelworks.com/billy-shoes-bootsa">billy shoes</a> [url=https://www.fracingsand.com/]fracingsand[/url]

  • gostpauldodge

    <a href="https://www.teamcansler.com/bridge-k%C5%AF%C5%BEe-ta%C5%A1ky-taskye">bridge k暖啪e ta拧ky</a> <a href="https://www.radassure.com/droid-turbo-pen%C4%9B%C5%BEenka-pouzdro-pouzdroe">droid turbo pen臎啪enka pouzdro</a> <a href="https://www.gableslimo.com/that-%C4%8Dern%C3%A1-%C5%A1aty-satyc">that 膷ern谩 拧aty</a> <a href="https://www.fixmybrewer.com/under-armour-donna-renegade-twist-berretto-cappelloa">under armour donna renegade twist berretto</a> <a href="https://www.peggymayer.com/denim-moccasins-bootsa">denim moccasins</a> <a href="https://www.peggymeyerdds.com/comfortable-damen-schuhe-for-standing-alle-day-stiefeld">comfortable damen schuhe for standing alle day</a> <a href="https://www.gostpauldodge.com/" >gostpauldodge</a> [url=https://www.gostpauldodge.com/]gostpauldodge[/url]

  • squeck

    <a href="http://www.docinsider.net/kansas-city-royals-sky-blue-hat-reviews-handd">kansas city royals sky blue hat reviews</a> <a href="http://www.fotowerft.net/black-and-white-air-jordan-10-shoes-spain-schoenende">black and white air jordan 10 shoes spain</a> <a href="http://www.brassmedia.net/unmatched-jonathan-weeks-jersey-houston-texans-46-men-white-game-jersey-nike-nfl-jersey-sale-nflb">unmatched jonathan weeks jersey houston texans 46 men white game jersey nike nfl jersey sale</a> <a href="http://www.sagerllp.net/black-white-red-jordan-super.fly-4-for-uk-billig-boote">black white red jordan super.fly 4 for uk billig</a> <a href="http://www.gastropol.net/nike-air-max-90-sort-wolf-grey-sneakerit">nike air max 90 sort wolf grey</a> <a href="http://www.sweetpee.net/ny-hat-grey-hatl">ny hat grey</a> squeck http://www.squeck.com/

  • radassure

    <a href="https://www.gestiarchivos.com/boys-marine-leder-sandalen-stiefeld">boys marine leder sandalen</a> <a href="https://www.fluffsstuffs.com/huawei-p20-phone-dekking-hoesjea">huawei p20 phone dekking</a> <a href="https://www.caldreamapp.com/vintage-jacksonville-jaguars-jersey-jerseyc">vintage jacksonville jaguars jersey</a> <a href="https://www.kwlagunabeach.com/teddy-blake-plecak-torebkid">teddy blake plecak</a> <a href="https://www.yenhost.com/detachable-phone-case-casec">detachable phone case</a> <a href="https://www.allgoodguide.com/victoria-secret-zilver-glitter-zak-tassenb">victoria secret zilver glitter zak</a> <a href="https://www.radassure.com/" >radassure</a> [url=https://www.radassure.com/]radassure[/url]

  • doodadsinc

    <a href="https://www.yourhorseshop.com/cover-samsung-s7-edge-clear-view-casec">cover samsung s7 edge clear view</a> <a href="https://www.peggymeyers.com/nike-azeda-2.0-tote-zak-tassenb">nike azeda 2.0 tote zak</a> <a href="https://www.cardaroopouch.com/iphone-xs-kryt-transparent-pouzdroe">iphone xs kryt transparent</a> <a href="https://www.jardimeubles.com/lime-sweater-%C5%A1aty-satyc">lime sweater 拧aty</a> <a href="https://www.lironlerman.com/outdoor-research-baseball-berretto-cappelloa">outdoor research baseball berretto</a> <a href="https://www.doodadsinc.com/witton-shoes-bootsa">witton shoes</a> <a href="https://www.doodadsinc.com/" >doodadsinc</a> [url=https://www.doodadsinc.com/]doodadsinc[/url]

发表评论

博客统计

访问量:5096715

博文总数:750 评论总数:909179

原创126 翻译20 转载604