마음의 안정을 찾기 위하여 - URL Decode 함수
2278077
785
743
관리자새글쓰기
태그위치로그방명록
별일없다의 생각
dawnsea's me2day/2010
색상(RGB)코드 추출기(Color...
Connection Generator/2010
최승호PD, '4대강 거짓말 검...
Green Monkey**/2010
Syng의 생각
syng's me2DAY/2010
천재 작곡가 윤일상이 기획,...
엘븐킹's Digital Factory/2010
URL Decode 함수
Delphi | 2007/09/18 17:13
RealThinClient 라이브러리를 이용하여 파일 업로드를 구현하는 도중, Post로 Parameter를 받아들이는 부분이 있었다.

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;

주석의 붉은 부분을 보면 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;

사용해보니 문제없이 잘 동작한다. Good Job~

위 함수의 출처는 http://www.delphidabbler.com/codesnip.php?action=named&routines=URLDecode&searchlogic=and&showsrc=1
2007/09/18 17:13 2007/09/18 17:13
Article tag list Go to top
View Comment 0
Trackback URL :: 이 글에는 트랙백을 보낼 수 없습니다
 
 
 
 
: [1] ... [706][707][708][709][710][711][712][713][714] ... [1317] :
«   2024/04   »
  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        
전체 (1317)
출판 준비 (0)
My-Pro... (41)
사는 ... (933)
블로그... (22)
My Lib... (32)
게임 ... (23)
개발관... (3)
Smart ... (1)
Delphi (93)
C Builder (0)
Object... (0)
VC, MF... (10)
Window... (1)
Open API (3)
Visual... (0)
Java, JSP (2)
ASP.NET (0)
PHP (5)
Database (12)
리눅스 (29)
Windows (25)
Device... (1)
Embedded (1)
게임 ... (0)
Web Se... (2)
Web, S... (21)
잡다한... (6)
프로젝트 (0)
Personal (0)
대통령... (13)
Link (2)