소프트웨어/ASP

URLEncode - JSP 연동

falconer 2007. 2. 14. 11:10

   Public Function URLEncode(URLStr)

    Dim sURL         '** 입력받은 URL 문자열
    Dim sBuffer      '** URL 인코딩 처리 중 URL 을 담을 버퍼 문자열
    Dim sTemp        '** 임시 문자열
    Dim cChar        '** URL 문자열 중 현재 인텍스의 문자
      
    Dim Index


    Dim lErrNum      '** 오류 번호
    Dim sErrSource   '** 오류 소스
    Dim sErrDesc     '** 소류 설명
    Dim sMsg         '** 오류 메세지
      
      
    sURL = Trim(URLStr)     '** URL 문자열을 얻는다.
    sBuffer = ""            '** 임시 버퍼용 문자열 변수 초기화.

      
    '******************************************************
    '* URL 인코딩 작업
    '******************************************************
      
    For Index = 1 To Len(sURL)
      
     '** 현재 인덱스의 문자를 얻는다.
     cChar = Mid(sURL, Index, 1)

     If cChar = "0" Or _
     (cChar >= "1" And cChar <= "9") Or _
     (cChar >= "a" And cChar <= "z") Or _
     (cChar >= "A" And cChar <= "Z") Or _
     cChar = "-" Or _
     cChar = "_" Or _
     cChar = "." Or _
     cChar = "*" Then

      '** URL 에 허용되는 문자들 :: 버퍼 문자열에 추가한다.
      sBuffer = sBuffer & cChar
              
     ElseIf cChar = " " Then
          
      '** 공백 문자 :: + 로 대체하여 버퍼 문자열에 추가한다.
      sBuffer = sBuffer & "+"
              
     Else
          
      '** URL 에 허용되지 않는 문자들 :: % 로 인코딩해서 버퍼 문자열에 추가한다.
      sTemp = CStr(Hex(Asc(cChar)))
              
      If Len(sTemp) = 4 Then

       sBuffer = sBuffer & "%" & Left(sTemp, 2) & "%" & Mid(sTemp, 3, 2)

      ElseIf Len(sTemp) = 2 Then

       sBuffer = sBuffer & "%" & sTemp

      End If
                  
     End If

    Next


    '** 결과를 리턴한다.
    URLEncode = sBuffer
      
      
   
   End Function

   '-----------------------------------------
   ' URLDecode (ASP NewsGroup whohwa)
   '-----------------------------------------
   Public Function URLDecode(byVal pURL)
    Dim vPos, result, tempHex, i
    pURL = Replace(pURL, "+", " ")
    result = ""

    for i = 1 to len(pURL)

     If Mid(pURL, i, 1) = "%" Then
      If LCase(Mid(pURL, i + 1, 1)) = "u" Then
       result = result & Chr(CLng("&H" & Mid(pURL, i + 2, 4)))
       i = i + 5
      Else

       If Mid(pURL, i + 3, 1) = "%" then
        tempHex = CLng("&H" & Mid(pURL, i + 1, 2))

        If tempHex > 127 Then
         result = result & _
         Chr(CLng("&H" & Mid(pURL, i + 1, 2)) * &H100 + CLng("&H" & Mid(pURL, i + 4, 2)))
         i = i + 5
        Else
         result = result & Chr(CLng("&H" & Mid(pURL, i + 1, 2)))
         i = i + 2
        End If
       Else
        tempHex = CLng("&H" & Mid(pURL, i + 1, 2))
        If tempHex > 127 Then
         result = result & Chr("&H" & Mid(pURL, i + 1, 2) & Cstr(Hex(Asc(Mid(pURL, i + 3, 1)))))
         i = i + 3
        Else
         result = result & Chr(CLng("&H" & Mid(pURL, i + 1, 2)))
         i = i + 2
        End If

       End If
      End If
     Else
      result = result & Mid(pURL, i, 1)
     End If
    next
    URLDecode = result
   End Function


   '-----------------------------------------
   ' URLEncode
   ' www.egocube.pe.kr
   '-----------------------------------------


 JSP서버 페이지 호출시에 유용하게 사용함