Delphi中处理URL编码解码

忘是亡心i 2022-03-30 12:22 553阅读 0赞

Delphi中处理URL编码解码

一、URL简单介绍
URL是网页的地址,比方 http://www.shanhaiMy.com。

Web 浏览器通过 URL 从 web server请求页面。
因为 URL字符串经常会包括非ASCII字符。URL在传输过程中,往往出现错误。因此。能够将非字符串字符。让一些特殊ASCII字符组合。取代非ASCII字符。

这就是编码转换。当字符串传输后,能够返回原RUL字符串(解码)。

URL仅仅能使用 ASCII 字符集来通过因特网进行发送。

URL编码,就是会将RUL字符转换为可通过因特网传输的格式。

URL编码使用“%”其后尾随两位的十六进制数来替换非 ASCII 字符。

比方“®”用“%A9”取代。

URL不能包括空格。URL编码通常使用“+”来替换空格。

二、RUL编码与解码
1、uses HttpApp;

2、编码。先UTF8编码。然后再URL编码。
S2:=HttpEncode(UTF8Encode(S1));

3、解码。先URL解码。然后再UTF8解码。
S1:=UTF8Decode(HttpDecode(S2));

RUL“编码”与“解码”操作:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,HttpApp,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Button1: TButton;
Button2: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
procedure FormShow(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormShow(Sender: TObject);
begin
Edit1.Text:=’http://img21.imgtn.bdimg.com/it/u=2964593467,576387895&fm=21&gp=0.jpg‘;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
edit2.Text:=HttpEncode(UTF8Encode(Edit1.text));
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
edit3.Text:=UTF8Decode(HttpDecode(edit2.Text));
end;

end.

发表评论

表情:
评论列表 (有 0 条评论,553人围观)

还没有评论,来说两句吧...

相关阅读

    相关 URL编码解码

    前言 最近在使用office online的时候,通常地址栏给出的一大串地址看不太懂是啥意思,简单查询了一下是和编码与解码的问题。 举个例子 > [http://