您的位置:首页 > 编程语言 > Delphi

Delphi中处理URL编码解码

2015-08-22 16:15 453 查看
Delphi中处理URL编码解码

一、URL简介

URL是网页的地址,比如 http://www.shanhaiMy.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编码。

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: