博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# UrlEncoding
阅读量:4519 次
发布时间:2019-06-08

本文共 3287 字,大约阅读时间需要 10 分钟。

一个类来编码URL ,.net 自带的 HttpUtility.UrlEncode, Uri.EscapeUriString 都不能满足超长度的字符串编码,根据官方文档知道 stringToEscape 的长度超过 32766 个字符。

于是用下面这个类就好了:

public static class Url{    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 + 0x30);        }        return (char)((n - 10) + 0x61);    }    public static byte[] UrlEncodeBytesToBytes(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++] = 0x2b;            }            else            {                buffer[num4++] = 0x25;                buffer[num4++] = (byte)IntToHex((num6 >> 4) & 15);                buffer[num4++] = (byte)IntToHex(num6 & 15);            }        }        return buffer;    }    public static byte[] UrlEncodeToBytes(string str)    {        if (str == null)        {            return null;        }        return UrlEncodeToBytes(str, Encoding.UTF8);    }    public static byte[] UrlEncodeToBytes(string str, Encoding e)    {        if (str == null)        {            return null;        }        byte[] bytes = e.GetBytes(str);        return UrlEncodeToBytes(bytes);    }    public static byte[] UrlEncodeToBytes(byte[] bytes)    {        if (bytes == null)        {            return null;        }        return UrlEncodeBytesToBytes(bytes, 0, bytes.Length, false);    }    public static string UrlEncode(string str, Encoding e)    {        if (str == null)        {            return null;        }        return Encoding.ASCII.GetString(UrlEncodeToBytes(str, e));    }    public static string UrlEncode(string str)    {        if (str == null)        {            return null;        }        return UrlEncode(str, Encoding.UTF8);    }    public static string UrlEncode(byte[] bytes)    {        if (bytes == null)        {            return null;        }        return Encoding.ASCII.GetString(UrlEncodeToBytes(bytes));    }}

//-----------------------------------------------------------

//调用:Url.UrlEncode(s);
//---------------------------------------------------------

转载于:https://www.cnblogs.com/wgscd/articles/7357489.html

你可能感兴趣的文章
解决请求筛选模块被配置为拒绝包含的查询字符串过长的请求
查看>>
PHP基础
查看>>
Oracle 的ORION工具简单使用
查看>>
局域网永恒之蓝病毒发包的解决方案之二
查看>>
红帽旗下Linux的版本说明RedHat、CentOS、Fedora、OEL等
查看>>
[转载]莫欺少年穷-----作者文笔真好
查看>>
jQuery快速入门知识重点
查看>>
写作(1~3)
查看>>
Hadoop数据类型
查看>>
LR中,URL -based script与HTML -based script区别
查看>>
清除IE中Ajax缓存,Chrome不需要
查看>>
日期时间格式化方法,可以格式化年、月、日、时、分、秒、周
查看>>
PairWork-电梯调度程序结对编程【附加题】
查看>>
Ext.Net学习笔记12:Ext.Net GridPanel Filter用法
查看>>
陪伴我走过春夏秋冬的校园
查看>>
bind()与connect()——计网中socket的使用
查看>>
ASP.NET WebApi 入门
查看>>
我想成为坐在路边鼓掌的人
查看>>
Html页面中Flash的播放,并利用JS变换Flash
查看>>
生成一条短信记录
查看>>