Delphi中处理URL编码解码

来源:互联网 发布:酷牛网络我的世界 编辑:程序博客网 时间:2024/05/21 15:47

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.

 

0 0
原创粉丝点击