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.