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编码 # 用来标志特定的文档位置 % % 对特殊字符进行编码 % & 分隔 ...
随机推荐
- spring-cloud:熔断监控Hystrix Dashboard和Turbine的示例
1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 2.GITHUB地址 https://github.com/nbfujx/springCl ...
- 4412 RS485
一.485硬件原理 差分对传输数据的原理 IO数据的传输→差分对 rs232传输的距离在15米以下,RS485传输距离是几十米到1000米以上 为什么485可以传输这么远 差分对的机制可以降低电磁场的 ...
- java 节点流(字符流,字节流)和包装流(缓冲流,转换流)
结点流:直接对File类进行操作的文件流 package stream; import java.io.File; import java.io.FileNotFoundException; impo ...
- paper 160:python 知识点概要 更新ing
1.python json http://www.runoob.com/python/python-json.html Python的json模块提供了一种很简单的方式来编码和解码JSON数据. 其 ...
- flutter中的命名路由
命名路由是区别于基本路由的一种存在,方便于大型项目中路由的统一管理,现在,在前面基本路由的项目基础上实现实现命名路由. 使用步骤 路由配置 命名路由在使用前,需要在根组件main.dart中进行简单的 ...
- Java JsonPath grab InvalidPathException in code, you must be catching Java 7's java.nio.file.InvalidPathException instead of JsonPath's com.jayway.jsonpath.InvalidPathExceptio
I am using JsonPath and am able to parse my data and get the values when the path provided is correc ...
- Dealing with exceptions thrown in Application_Start()
https://blog.richardszalay.com/2007/03/08/dealing-with-exceptions-thrown-in-application_start/ One a ...
- 测开之路四十:jQuery基本用法
从cdn引入jQuery库:https://www.bootcdn.cn/,搜索jQuery 在html里面(使用之前计算器的脚本),把复制的标签粘贴到引入js标签的前面:<script src ...
- upc组队赛16 GCDLCM 【Pollard_Rho大数质因数分解】
GCDLCM 题目链接 题目描述 In FZU ACM team, BroterJ and Silchen are good friends, and they often play some int ...
- c# HttpListener 使用
与 IIS 上发布网站相比,使用 HttpListener 编程的程序更加轻量化,易于发布和更新.配合 Thread 或 Task 类也可满足一定的并发. https://docs.microsoft ...