URL encoding/decoding: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: URL encoding/decoding
| Old | New | Line | |
|---|---|---|---|
| 1 | + | **Question:** I need to pass some arbitrary text as a query string to a URL that will be opened in the default browser via OpenURL. Obviously, the query string will need to be URL-encoded - is there a standard function somewhere for this? | |
| 2 | + | ||
| 3 | + | **Answer-1:** You have httpprotocol.HTTPEncode and httpprotocol.HTTPDecode. | |
| 4 | + | ||
| 5 | + | ``` Pascal | |
| 6 | + | uses | |
| 7 | + | httpprotocol; | |
| 8 | + | var | |
| 9 | + | A, B: string; | |
| 10 | + | begin | |
| 11 | + | A := 'http://www.somesite.com/page1.asp?id=65'; | |
| 12 | + | B := HTTPEncode(A); | |
| 13 | + | A := HTTPDecode(B); | |
| 14 | + | ``` | |
| 15 | + | ||
| 16 | + | **Answer-2:** Here are ready functions, written by forum member BenJones. | |
| 17 | + | ||
| 18 | + | ``` Pascal | |
| 19 | + | function EncodeUrl(const url: string): string; | |
| 20 | + | var | |
| 21 | + | x: integer; | |
| 22 | + | sBuff: string; | |
| 23 | + | const | |
| 24 | + | SafeMask = ['A'..'Z', '0'..'9', 'a'..'z', '*', '@', '.', '_', '-']; | |
| 25 | + | begin | |
| 26 | + | //Init | |
| 27 | + | sBuff := ''; | |
| 28 | + | ||
| 29 | + | for x := 1 to Length(url) do | |
| 30 | + | begin | |
| 31 | + | //Check if we have a safe char | |
| 32 | + | if url[x] in SafeMask then | |
| 33 | + | begin | |
| 34 | + | //Append all other chars | |
| 35 | + | sBuff := sBuff + url[x]; | |
| 36 | + | end | |
| 37 | + | else if url[x] = ' ' then | |
| 38 | + | begin | |
| 39 | + | //Append space | |
| 40 | + | sBuff := sBuff + '+'; | |
| 41 | + | end | |
| 42 | + | else | |
| 43 | + | begin | |
| 44 | + | //Convert to hex | |
| 45 | + | sBuff := sBuff + '%' + IntToHex(Ord(url[x]), 2); | |
| 46 | + | end; | |
| 47 | + | end; | |
| 48 | + | ||
| 49 | + | Result := sBuff; | |
| 50 | + | end; | |
| 51 | + | ||
| 52 | + | function DecodeUrl(const url: string): string; | |
| 53 | + | var | |
| 54 | + | x: integer; | |
| 55 | + | ch: string; | |
| 56 | + | sVal: string; | |
| 57 | + | Buff: string; | |
| 58 | + | begin | |
| 59 | + | //Init | |
| 60 | + | Buff := ''; | |
| 61 | + | x := 1; | |
| 62 | + | while x <= Length(url) do | |
| 63 | + | begin | |
| 64 | + | //Get single char | |
| 65 | + | ch := url[x]; | |
| 66 | + | ||
| 67 | + | if ch = '+' then | |
| 68 | + | begin | |
| 69 | + | //Append space | |
| 70 | + | Buff := Buff + ' '; | |
| 71 | + | end | |
| 72 | + | else if ch <> '%' then | |
| 73 | + | begin | |
| 74 | + | //Append other chars | |
| 75 | + | Buff := Buff + ch; | |
| 76 | + | end | |
| 77 | + | else | |
| 78 | + | begin | |
| 79 | + | //Get value | |
| 80 | + | sVal := Copy(url, x + 1, 2); | |
| 81 | + | //Convert sval to int then to char | |
| 82 | + | Buff := Buff + char(StrToInt('$' + sVal)); | |
| 83 | + | //Inc counter by 2 | |
| 84 | + | Inc(x, 2); | |
| 85 | + | end; | |
| 86 | + | //Inc counter | |
| 87 | + | Inc(x); | |
| 88 | + | end; | |
| 89 | + | //Return result | |
| 90 | + | Result := Buff; | |
| 91 | + | end; | |
| 92 | + | ||
| 93 | + | procedure TForm1.Button1Click(Sender: TObject); | |
| 94 | + | var | |
| 95 | + | A, B: string; | |
| 96 | + | begin | |
| 97 | + | A := 'http://www.somesite.com/page1.asp?id=65'; | |
| 98 | + | //Encode url | |
| 99 | + | B := EncodeUrl(A); | |
| 100 | + | //Show Encoded url | |
| 101 | + | ShowMessage(B); | |
| 102 | + | //Show deoded url | |
| 103 | + | ShowMessage(DecodeUrl(B)); | |
| 104 | + | end; | |
| 105 | + | ``` | |
| 106 | + | ||
| 107 | + | --- | |
| 108 | + | *From the Free Pascal wiki, page "URL encoding/decoding", as it stood on 2021-12-11 after a change by Alextp, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
