Via Cà Matta 2 - Peschiera Borromeo (MI)
+39 02 00704272
info@synaptica.info

Hand made Url Encode or Decode

Digital Innovation Partner

Hand made Url Encode or Decode

Sometime happen that you need to encode or decode an url manually , so not soo much characters need to be converteted to encode or decode an url string.
In this post some example you can drag and use or modify.

In recent version of Delphi you can use standard Delphi routine contained in the “NetEncoding” library like:

uses NetEncoding;

....

function myEncoder(srcStr : String) : String;
begin
 result := TNetEncoding.url.encode(srcStr);
end;

If you do this manually, in the 99% of cases you need only convert the most common URI charecter (with percent as the start of escape char) :

Common characters after percent-encoding (ASCII or UTF-8 based)
< > ~ . " { } | \ - ` _ ^ % space
%3C %3E %7E %2E %22 %7B %7D %7C %5C %2D %60 %5F %5E %25 %20

for more information you can read this article on wikipedia : http://en.wikipedia.org/wiki/Percent-encoding

A Delphi example of URL encode :

function  TDmMain.UrlEncode(StrUrl : String) : String;
begin
  Result := StrUrl;
 Result := StringReplace(Result,'%','%25',[rfReplaceAll]);
 Result := StringReplace(Result,' ','%20',[rfReplaceAll]);
 Result := StringReplace(Result,'<','%3C',[rfReplaceAll]);
 Result := StringReplace(Result,'>','%3E',[rfReplaceAll]);
 Result := StringReplace(Result,'~','%7E',[rfReplaceAll]);
// Result := StringReplace(Result,'.','%2E',[rfReplaceAll]);
 Result := StringReplace(Result,'"','%22',[rfReplaceAll]);
 Result := StringReplace(Result,'{','%7B',[rfReplaceAll]);
 Result := StringReplace(Result,'}','%7D',[rfReplaceAll]);
 Result := StringReplace(Result,'|','%7C',[rfReplaceAll]);
 Result := StringReplace(Result,'\','%5C',[rfReplaceAll]);
 Result := StringReplace(Result,'-','%2D',[rfReplaceAll]);
 Result := StringReplace(Result,'`','%60',[rfReplaceAll]);
 Result := StringReplace(Result,'_','%5F',[rfReplaceAll]);
 Result := StringReplace(Result,'^','%5E',[rfReplaceAll]);
end;



An Oracle PL/SQL Url decoding example :

CREATE OR REPLACE FUNCTION "F_URL_DECODE" (STR_IN VARCHAR) RETURN VARCHAR IS
tmp_Var VARCHAR(4000);
/******************************************************************************
NAME:       F_URL_DECODE
PURPOSE:

REVISIONS:
Ver        Date        Author           Description
---------  ----------  ---------------  ------------------------------------
1.0        12/01/2011          1. Created this function.

NOTES:

Automatically available Auto Replace Keywords:
Object Name:     F_URL_DECODE
Sysdate:         12/01/2011
Date and Time:   12/01/2011, 15.06.42, and 12/01/2011 15.06.42
Username:         (set in TOAD Options, Procedure Editor)
Table Name:       (set in the "New PL/SQL Object" dialog)

******************************************************************************/
BEGIN
tmp_Var := STR_IN;

TMP_VAR := REPLACE(TMP_VAR, '%24', '
);
TMP_VAR := REPLACE(TMP_VAR, '%26', '&');
TMP_VAR := REPLACE(TMP_VAR, '%2B', '+');
TMP_VAR := REPLACE(TMP_VAR, '%2C', ',');
TMP_VAR := REPLACE(TMP_VAR, '%2F', '/');
TMP_VAR := REPLACE(TMP_VAR, '%3A', ':');
TMP_VAR := REPLACE(TMP_VAR, '%3B', ';');
TMP_VAR := REPLACE(TMP_VAR, '%3D', '=');
TMP_VAR := REPLACE(TMP_VAR, '%3F', '?');
TMP_VAR := REPLACE(TMP_VAR, '%40', '@');
TMP_VAR := REPLACE(TMP_VAR, '%20', ' ');

-- CONSIDERO IL MINUS CASE

TMP_VAR := REPLACE(TMP_VAR, '%24', '
);
TMP_VAR := REPLACE(TMP_VAR, '%26', '&');
TMP_VAR := REPLACE(TMP_VAR, '%2b', '+');
TMP_VAR := REPLACE(TMP_VAR, '%2c', ',');
TMP_VAR := REPLACE(TMP_VAR, '%2f', '/');
TMP_VAR := REPLACE(TMP_VAR, '%3a', ':');
TMP_VAR := REPLACE(TMP_VAR, '%3b', ';');
TMP_VAR := REPLACE(TMP_VAR, '%3d', '=');
TMP_VAR := REPLACE(TMP_VAR, '%3f', '?');
TMP_VAR := REPLACE(TMP_VAR, '%40', '@');
TMP_VAR := REPLACE(TMP_VAR, '%20', ' ');
TMP_VAR := REPLACE(TMP_VAR, '%25', '%');

RETURN tmp_Var;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
WHEN OTHERS THEN
-- Consider logging the error and then re-raise
RAISE;
END F_URL_DECODE;
/

Tags: , , , ,

Lascia un commento