Delphi UTF编码 UTF8Encode、UTF8Decode、URLEncode、URLDecode
一、URL简介
URL是网页的地址,比如 http://www.cnblogs.com。Web 浏览器通过 URL 从 web 服务器请求页面。
由于URL字符串常常会包含非ASCII字符,URL在传输过程中,往往出现错误。因此,可以将非字符串字符,让一些特殊ASCII字符组合,代替非ASCII字符。这就是编码转换,当字符串传输后,可以返回原RUL字符串(解码)。
URL只能使用 ASCII 字符集来通过因特网进行发送。URL编码,就是会将RUL字符转换为可通过因特网传输的格式。
URL编码使用“%”其后跟随两位的十六进制数来替换非 ASCII 字符。比如“®”用“%A9”代替。
URL不能包含空格。URL编码通常使用“+”来替换空格。
二、RUL编码与解码
1、uses HttpApp;
2、编码,先UTF8编码,然后再URL编码,不然和标准的url_encode()编码结果不一致,查询结果自然不是预期的
S2 := HttpEncode(UTF8Encode(S1));
3、解码,先URL解码,然后再UTF8解码,否则结果是乱码。
S1 := UTF8Decode(HttpDecode(S2));
以上是内置函数调用
三、URLEncode、URLDecode
function URLDecode(const S: string): string;
var
Idx: Integer; // loops thru chars in string
Hex: string; // string of hex characters
Code: Integer; // hex character code (-1 on error)
begin
// Intialise result and string index
Result := '';
Idx := 1;
// Loop thru string decoding each character
while Idx <= Length(S) do
begin
case S[Idx] of
'%':
begin
// % should be followed by two hex digits - exception otherwise
if Idx <= Length(S) - 2 then
begin
// there are sufficient digits - try to decode hex digits
Hex := S[Idx+1] + S[Idx+2];
Code := SysUtils.StrToIntDef('$' + Hex, -1);
Inc(Idx, 2);
end
else
// insufficient digits - error
Code := -1;
// check for error and raise exception if found
if Code = -1 then
raise SysUtils.EConvertError.Create(
'Invalid hex digit in URL'
);
// decoded OK - add character to result
Result := Result + Chr(Code);
end;
'+':
// + is decoded as a space
Result := Result + ' '
else
// All other characters pass thru unchanged
Result := Result + S[Idx];
end;
Inc(Idx);
end;
end;
function URLEncode(const S: string; const InQueryString: Boolean): string;
var
Idx: Integer; // loops thru characters in string
begin
Result := '';
for Idx := 1 to Length(S) do
begin
case S[Idx] of
'A'..'Z', 'a'..'z', '0'..'9', '-', '_', '.':
Result := Result + S[Idx];
' ':
if InQueryString then
Result := Result + '+'
else
Result := Result + '%20';
else
Result := Result + '%' + SysUtils.IntToHex(Ord(S[Idx]), 2);
end;
end;
end;
Delphi UTF编码 UTF8Encode、UTF8Decode、URLEncode、URLDecode的更多相关文章
- python中Url链接编码处理(urlencode,urldecode)
做完了flask-web应用,这几天想用爬虫做个好玩的电影链接整合器,平时找电影都是在dytt或者dy2018之类的网站,在用dytt搜索电影<美国队长时>,发现他的搜索链接是这样的:ht ...
- 利用zxing制作彩色,高容错,支持中文等UTF编码的QR二维码图片
利用zxing制作彩色,高容错,支持中文等UTF编码的QR二维码图片.代码如下 import java.awt.Color;import java.io.File;import java.util.H ...
- UTF编码问题小结
在编程当中经常出现乱码的问题,而由此一般会引发很多惨剧,如读文件不成功.用户名显示乱码等,所以端午节抽了一小点时间好好看了一下编码问题,以备遗忘. 首先是中文编码,除了台湾和香港常用的BIG5,国内大 ...
- UTF编码检测
最近工作上正好需要进行UTF编码检测,自己写了一个,分享给大家,希望可以帮得上有需要用的朋友 public bool isUtf8(byte[] rawText) { bool result = tr ...
- 对编码内容多次UrlDecode
对编码内容多次UrlDecode,并不会影响最终结果. 尝试阅读了微软的源代码,不过不容易读懂. 网址:https://referencesource.microsoft.com/#System/ne ...
- 终端命令对字符串进行sha1、md5、base64、urlencode/urldecode
sha1.md5.base64 mac $ echo -n foo|shasum 0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33 - $ 2c26b46b68ffc6 ...
- UNICODE UTF编码方式解析
先明确几个概念 基础概念部分 1.字符编码方式CEF(Character Encoding Form) 对符号进行编码,便于处理与显示 常用的编码方式有 GB2312(汉字国标码 2字节) ASCII ...
- Delphi 解决Utf8ToAnsi和Utf8DeCode转换编码为空的问题
//delphi DecodeUtf8Str解决系统自带UTF8解码缺陷 function DecodeUtf8Str(const S: UTF8String): WideString; var le ...
- lua urlencode urldecode URL编码
URL编码其实就是对一些字符转义为%加上该字符对应ASCII码的二位十六进制形式. 如: 字符 特殊字符的含义 URL编码 # 用来标志特定的文档位置 % % 对特殊字符进行编码 % & 分隔 ...
随机推荐
- Lock之ReentrantLock及实现生产者消费者和死锁
Lock是顶层接口,它的实现逻辑并未用到synchronized,而是利用了volatile的可见性.ReentrantLock对了Lock接口的实现主要依赖了Sync,而Sync继承了 Abstra ...
- 4412 linux延时和时间
基本知识 • linux中延时函数很简单,却经常用到• 在操作系统中和单片机处理延时方式就完全不一样了,不可能是使用for循环浪费系统资源.而是有专门的接口函数• linux系统编程中常用的延时函数: ...
- Android setXfermode 模式
参考:http://onewayonelife.iteye.com/blog/1169176 setXfermode 设置两张图片相交时的模式 我们知道 在正常的情况下,在已有的图像上绘图将会在 ...
- 2018-2019-2 20175120 实验四《Android程序设计》实验报告
任务一:Android Studio的安装测试 任务要求:参考<Java和Android开发学习指南(第二版)(EPUBIT,Java for Android 2nd)>第二十四章: 参考 ...
- mysql使用crontab定时备份
1, 安装crontab yum install vixie-cron yum install crontabs 说明:vixie-cron软件包是cron的主程序:crontabs软件包是用来安装. ...
- SercletConfig 详解
ServletConfig:从一个servlet被实例化后,对任何客户端在任何时候访问有效,但仅对本servlet有效,一个servlet的ServletConfig对象不能被另一个servlet访问 ...
- kubernetes安装部署
1.根据系统内核情况,选择对应的ali云上的镜像,作为仓库的路径指向来配置k8s https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes- ...
- PAT_A1073#Scientific Notation
Source: PAT A1073 Scientific Notation (20 分) Description: Scientific notation is the way that scient ...
- MSF——Meterpreter(三)
MSF系列: MSF——基本使用和Exploit模块(一) MSF——Payload模块(二) MSF——Meterpreter(三) MSF——信息收集(四) 一.简介 Meterpreter是Me ...
- Visual Assist 10.9.2248 破解版(支持VS2017) 转载
自己在Windows10下同时安装了VS2017和VS2013,先装的VS2017和Visual Assist,后装的VS2013,发现VS2013中没显示Visual Assist,Google了一 ...