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:
1 2 3 4 5 6 7 8 |
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) :
< |
> |
~ |
. |
" |
{ |
} |
|
\ |
- |
` |
_ |
^ |
% |
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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
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; / |