中文问题-Mobile-UrlEncode
中文问题-Mobile-UrlEncode
在做断点续传的时候遇到(cf)不支持中文的问题,主要是没有UrlEncode该方法,后来几经周折解决了,和大家分享,希望对遇到该问题的朋友有所帮助。
Code
using System;
using System.Collections.Generic;
using System.Text;
namespace BreakPointResumeWindowsCeHttp
{
/// <summary>
/// EncryptMethod
/// </summary>
public class EncryptMethod
{
private EncryptMethod()
{
}
/// <summary>
/// UrlEncode
/// </summary>
/// <param name="str">源字符串</param>
/// <param name="e">Encoding</param>
/// <returns>编码后的字符串</returns>
public static string UrlEncode(string str, Encoding e)
{
if (str == null)
{
return null;
}
byte[] buffer = UrlEncodeToBytes(str, e);
return Encoding.ASCII.GetString(buffer, 0, buffer.Length);
}
/// <summary>
/// UrlEncodeToBytes
/// </summary>
/// <param name="str">str</param>
/// <param name="e">Encoding</param>
/// <returns>UrlEncodeToBytes</returns>
public static byte[] UrlEncodeToBytes(string str, Encoding e)
{
if (str == null)
{
return null;
}
byte[] buffer1 = e.GetBytes(str);
return UrlEncodeBytesToBytesInternal(buffer1, 0, buffer1.Length, false);
}
private static byte[] UrlEncodeBytesToBytesInternal(byte[] bytes, int offset, int count, bool alwaysCreateReturnValue)
{
int num1 = 0;
int num2 = 0;
for (int num3 = 0; num3 < count; num3++)
{
char ch1 = (char)bytes[offset + num3];
if (ch1 == ' ')
{
num1++;
}
else if (!IsSafe(ch1))
{
num2++;
}
}
if ((!alwaysCreateReturnValue && (num1 == 0)) && (num2 == 0))
{
return bytes;
}
byte[] buffer1 = new byte[count + (num2 * 2)];
int num4 = 0;
for (int num5 = 0; num5 < count; num5++)
{
byte num6 = bytes[offset + num5];
char ch2 = (char)num6;
if (IsSafe(ch2))
{
buffer1[num4++] = num6;
}
else if (ch2 == ' ')
{
buffer1[num4++] = 0x2b;
}
else
{
buffer1[num4++] = 0x25;
buffer1[num4++] = (byte)IntToHex((num6 >> 4) & 15);
buffer1[num4++] = (byte)IntToHex(num6 & 15);
}
}
return buffer1;
}
private static char IntToHex(int n)
{
if (n <= 9)
{
return (char)((ushort)(n + 0x30));
}
return (char)((ushort)((n - 10) + 0x61));
}
internal 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;
}
/// <summary>
/// Return base64 version of string.
/// </summary>
public static string Encrypt(string text)
{
try
{
byte[] bytes = Encoding.ASCII.GetBytes(text);
return Convert.ToBase64String(bytes, 0, bytes.Length);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
return String.Empty;
}
}
/// <summary>
/// Return string version of base64 string.
/// </summary>
public static string Decrypt(string text)
{
try
{
byte[] bytes = Convert.FromBase64String(text);
return Encoding.ASCII.GetString(bytes, 0, bytes.Length);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
return String.Empty;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace BreakPointResumeWindowsCeHttp
{
/// <summary>
/// EncryptMethod
/// </summary>
public class EncryptMethod
{
private EncryptMethod()
{
}
/// <summary>
/// UrlEncode
/// </summary>
/// <param name="str">源字符串</param>
/// <param name="e">Encoding</param>
/// <returns>编码后的字符串</returns>
public static string UrlEncode(string str, Encoding e)
{
if (str == null)
{
return null;
}
byte[] buffer = UrlEncodeToBytes(str, e);
return Encoding.ASCII.GetString(buffer, 0, buffer.Length);
}
/// <summary>
/// UrlEncodeToBytes
/// </summary>
/// <param name="str">str</param>
/// <param name="e">Encoding</param>
/// <returns>UrlEncodeToBytes</returns>
public static byte[] UrlEncodeToBytes(string str, Encoding e)
{
if (str == null)
{
return null;
}
byte[] buffer1 = e.GetBytes(str);
return UrlEncodeBytesToBytesInternal(buffer1, 0, buffer1.Length, false);
}
private static byte[] UrlEncodeBytesToBytesInternal(byte[] bytes, int offset, int count, bool alwaysCreateReturnValue)
{
int num1 = 0;
int num2 = 0;
for (int num3 = 0; num3 < count; num3++)
{
char ch1 = (char)bytes[offset + num3];
if (ch1 == ' ')
{
num1++;
}
else if (!IsSafe(ch1))
{
num2++;
}
}
if ((!alwaysCreateReturnValue && (num1 == 0)) && (num2 == 0))
{
return bytes;
}
byte[] buffer1 = new byte[count + (num2 * 2)];
int num4 = 0;
for (int num5 = 0; num5 < count; num5++)
{
byte num6 = bytes[offset + num5];
char ch2 = (char)num6;
if (IsSafe(ch2))
{
buffer1[num4++] = num6;
}
else if (ch2 == ' ')
{
buffer1[num4++] = 0x2b;
}
else
{
buffer1[num4++] = 0x25;
buffer1[num4++] = (byte)IntToHex((num6 >> 4) & 15);
buffer1[num4++] = (byte)IntToHex(num6 & 15);
}
}
return buffer1;
}
private static char IntToHex(int n)
{
if (n <= 9)
{
return (char)((ushort)(n + 0x30));
}
return (char)((ushort)((n - 10) + 0x61));
}
internal 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;
}
/// <summary>
/// Return base64 version of string.
/// </summary>
public static string Encrypt(string text)
{
try
{
byte[] bytes = Encoding.ASCII.GetBytes(text);
return Convert.ToBase64String(bytes, 0, bytes.Length);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
return String.Empty;
}
}
/// <summary>
/// Return string version of base64 string.
/// </summary>
public static string Decrypt(string text)
{
try
{
byte[] bytes = Convert.FromBase64String(text);
return Encoding.ASCII.GetString(bytes, 0, bytes.Length);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
return String.Empty;
}
}
}
}
包括:
CompressionBytes:利用SharpZipLib压缩和解压缩
HttpUtility:UrlDecode、UrlEncode
Utils:LogError
更新后的:下载
为成功找方法,不为失败找借口!