「C#でUrlEncodeとUrlDecode」の版間の差分

提供: MonoBook
ナビゲーションに移動 検索に移動
imported>Administrator
imported>Administrator
 
(同じ利用者による、間の4版が非表示)
1行目: 1行目:
C#というか.NETにはUrlEncodeとUrlDecodeの統一的な方法がない。標準的に行う方法は、バージョンごと、プラットフォームごとに方法が異なりすぎて悲惨なことになっている。
+
C#というか[[.NET]]にはUrlEncodeとUrlDecodeの統一的な方法がない。標準的に行う方法は、バージョンごと、プラットフォームごとに方法が異なりすぎて悲惨なことになっている。
 +
 
 +
こんなもんStringクラスの拡張メソッドでいいだろ。アホか。
  
 
<syntaxhighlight lang="csharp">
 
<syntaxhighlight lang="csharp">
  
  
public static string UrlEncode( string s , Encoding enc )
+
    public static class StringExtensions
{
+
    {
var rt = new StringBuilder();
+
        public static string UrlEncode(this string str, Encoding enc)
foreach ( byte i in enc.GetBytes( s ) )
+
        {
{
+
            var rt = new StringBuilder();
if ( i == 0x20 )
+
            foreach (byte i in enc.GetBytes(str))
{
+
            {
rt.Append( '+' );
+
                if (i == 0x20)
}
+
                {
else if ( (0x30 <= i && i <= 0x39) || (0x41 <= i && i <= 0x5a) || (0x61 <= i && i <= 0x7a) )
+
                    rt.Append('+');
{
+
                }
rt.Append( (char)i );
+
                else if ((0x30 <= i && i <= 0x39) || (0x41 <= i && i <= 0x5a) || (0x61 <= i && i <= 0x7a))
}
+
                {
else
+
                    rt.Append((char)i);
{
+
                }
rt.Append( "%" + i.ToString( "X2" ) );
+
                else
}
+
                {
}
+
                    rt.Append("%" + i.ToString("X2"));
return rt.ToString();
+
                }
}
+
            }
 +
            return rt.ToString();
 +
        }
 +
 
 +
        public static string UrlDecode(this string str, Encoding enc)
 +
        {
 +
            var bytes = new List<byte>();
 +
            for (int i = 0; i < str.Length; i++)
 +
            {
 +
                char c = str[i];
 +
                if (c == '%')
 +
                {
 +
                    bytes.Add((byte)int.Parse(str[++i].ToString() + str[++i].ToString(), System.Globalization.NumberStyles.HexNumber));
 +
                }
 +
                else if (c == '+')
 +
                {
 +
                    bytes.Add((byte)0x20);
 +
                }
 +
                else
 +
                {
 +
                    bytes.Add((byte)c);
 +
                }
 +
            }
 +
            return enc.GetString(bytes.ToArray(), 0, bytes.Count);
 +
        }
 +
    }
  
 
</syntaxhighlight>
 
</syntaxhighlight>
<syntaxhighlight lang="csharp">
 
public static string UrlDecode( string s , Encoding enc )
 
{
 
var bytes = new List<byte>();
 
for ( int i = 0; i < s.Length; i++ )
 
{
 
char c = s[i];
 
if ( c == '%' )
 
{
 
bytes.Add( (byte)int.Parse( s[++i].ToString() + s[++i].ToString() , System.Globalization.NumberStyles.HexNumber ) );
 
}
 
else if ( c == '+' )
 
{
 
bytes.Add( (byte)0x20 );
 
}
 
else
 
{
 
bytes.Add( (byte)c );
 
}
 
}
 
return enc.GetString( bytes.ToArray() , 0 , bytes.Count );
 
}
 
  
</syntaxhighlight>
+
[[category: .NET]]

2018年8月14日 (火) 04:29時点における最新版

C#というか.NETにはUrlEncodeとUrlDecodeの統一的な方法がない。標準的に行う方法は、バージョンごと、プラットフォームごとに方法が異なりすぎて悲惨なことになっている。

こんなもんStringクラスの拡張メソッドでいいだろ。アホか。

    public static class StringExtensions
    {
        public static string UrlEncode(this string str, Encoding enc)
        {
            var rt = new StringBuilder();
            foreach (byte i in enc.GetBytes(str))
            {
                if (i == 0x20)
                {
                    rt.Append('+');
                }
                else if ((0x30 <= i && i <= 0x39) || (0x41 <= i && i <= 0x5a) || (0x61 <= i && i <= 0x7a))
                {
                    rt.Append((char)i);
                }
                else
                {
                    rt.Append("%" + i.ToString("X2"));
                }
            }
            return rt.ToString();
        }

        public static string UrlDecode(this string str, Encoding enc)
        {
            var bytes = new List<byte>();
            for (int i = 0; i < str.Length; i++)
            {
                char c = str[i];
                if (c == '%')
                {
                    bytes.Add((byte)int.Parse(str[++i].ToString() + str[++i].ToString(), System.Globalization.NumberStyles.HexNumber));
                }
                else if (c == '+')
                {
                    bytes.Add((byte)0x20);
                }
                else
                {
                    bytes.Add((byte)c);
                }
            }
            return enc.GetString(bytes.ToArray(), 0, bytes.Count);
        }
    }