Received이벤트에서 Request.Params.AddText(Read); 를 하여 Params를 하게 된후 추후 해당 파라미터를 사용하고자 하는 경우
Request.Params.Value['text']를 통해 "text"의 내용을 확인하게 되면
내가 보낸 내용 : 프리즌 브레이크 (Prison Break)
실제 분리된 내용 : %C7%C1%B8%AE%C1%F0+%BA%EA%B7%B9%C0%CC%C5%A9+%28Prison+Break%29
URL Encoding에 의해 전달되어진 값이 알아볼 수 없는 형태로 처리되어 있다.
이를 해결하기 위하여 URLDecode루틴을 수행하게 되는데, 아래의 코드는 Indy9에서 발췌한 내용이다.
class function URLDecode(ASrc: string): string;
var
i: integer;
ESC: string[2];
CharCode: integer;
begin
Result := ''; {Do not Localize}
// S.G. 27/11/2002: Spaces is NOT to be encoded as "+".
// S.G. 27/11/2002: "+" is a field separator in query parameter, space is...
// S.G. 27/11/2002: well, a space
// ASrc := StringReplace(ASrc, '+', ' ', [rfReplaceAll]); {do not localize}
i := 1;
while i <= Length(ASrc) do begin
if ASrc[i] <> '%' then begin {do not localize}
Result := Result + ASrc[i]
end else begin
Inc(i); // skip the % char
ESC := Copy(ASrc, i, 2); // Copy the escape code
Inc(i, 1); // Then skip it.
try
CharCode := StrToInt('$' + ESC); {do not localize}
if (CharCode > 0) and (CharCode < 256) then begin
Result := Result + Char(CharCode);
end;
except end;
end;
Inc(i);
end;
end;
var
i: integer;
ESC: string[2];
CharCode: integer;
begin
Result := ''; {Do not Localize}
// S.G. 27/11/2002: Spaces is NOT to be encoded as "+".
// S.G. 27/11/2002: "+" is a field separator in query parameter, space is...
// S.G. 27/11/2002: well, a space
// ASrc := StringReplace(ASrc, '+', ' ', [rfReplaceAll]); {do not localize}
i := 1;
while i <= Length(ASrc) do begin
if ASrc[i] <> '%' then begin {do not localize}
Result := Result + ASrc[i]
end else begin
Inc(i); // skip the % char
ESC := Copy(ASrc, i, 2); // Copy the escape code
Inc(i, 1); // Then skip it.
try
CharCode := StrToInt('$' + ESC); {do not localize}
if (CharCode > 0) and (CharCode < 256) then begin
Result := Result + Char(CharCode);
end;
except end;
end;
Inc(i);
end;
end;
주석의 붉은 부분을 보면 StringReplace로 '+'문자를 공백으로 치환할것을 권유하는듯 한데,
해당 함수에서 "+"를 처리하고 있지 않기 때문이다.
위의 내용이 반영된 함수를 인터넷 서핑중에 득템~
function URLDecode(const S: string): string;
var
Idx: Integer; // loops thru chars in string
Hex: string; // string of hex characters
Code: Integer; // hex character code (-1 on error)
begin
// Intialise result and string index
Result := '';
Idx := 1;
// Loop thru string decoding each character
while Idx <= Length(S) do
begin
case S[Idx] of
'%':
begin
// % should be followed by two hex digits - exception otherwise
if Idx <= Length(S) - 2 then
begin
// there are sufficient digits - try to decode hex digits
Hex := S[Idx+1] + S[Idx+2];
Code := SysUtils.StrToIntDef('$' + Hex, -1);
Inc(Idx, 2);
end
else
// insufficient digits - error
Code := -1;
// check for error and raise exception if found
if Code = -1 then
raise SysUtils.EConvertError.Create(
'Invalid hex digit in URL'
);
// decoded OK - add character to result
Result := Result + Chr(Code);
end;
'+':
// + is decoded as a space
Result := Result + ' '
else
// All other characters pass thru unchanged
Result := Result + S[Idx];
end;
Inc(Idx);
end;
end;
var
Idx: Integer; // loops thru chars in string
Hex: string; // string of hex characters
Code: Integer; // hex character code (-1 on error)
begin
// Intialise result and string index
Result := '';
Idx := 1;
// Loop thru string decoding each character
while Idx <= Length(S) do
begin
case S[Idx] of
'%':
begin
// % should be followed by two hex digits - exception otherwise
if Idx <= Length(S) - 2 then
begin
// there are sufficient digits - try to decode hex digits
Hex := S[Idx+1] + S[Idx+2];
Code := SysUtils.StrToIntDef('$' + Hex, -1);
Inc(Idx, 2);
end
else
// insufficient digits - error
Code := -1;
// check for error and raise exception if found
if Code = -1 then
raise SysUtils.EConvertError.Create(
'Invalid hex digit in URL'
);
// decoded OK - add character to result
Result := Result + Chr(Code);
end;
'+':
// + is decoded as a space
Result := Result + ' '
else
// All other characters pass thru unchanged
Result := Result + S[Idx];
end;
Inc(Idx);
end;
end;
사용해보니 문제없이 잘 동작한다. Good Job~
위 함수의 출처는 http://www.delphidabbler.com/codesnip.php?action=named&routines=URLDecode&searchlogic=and&showsrc=1