티스토리 뷰

Tech

Java - Url(Encoding, Decoding) Sample Code

밤백수 2023. 2. 14. 00:05

필요한 경우

  • oauth 인증시 redirect uri
  • query 파라미터로 /등 특수문자를 보낼 때

사용불가 캐릭터

  • / (forward slash)
  • ? (question mark)
  • & (ampersand)
  • = (equals sign)
  • % (percent sign)
  • + (plus sign)
  • # (hash or pound sign)
  • < (less than sign)
  • > (greater than sign)
  • : (colon)
  • , (comma)

Sample Code

URL Encode

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class UrlEncoderExample {
  public static void main(String[] args) throws UnsupportedEncodingException {
    String originalString = "urlcodode this string "https://www.tistory.com/redirect";
    String encodedString = URLEncoder.encode(originalString, "UTF-8");
    System.out.println("Original string: " + originalString);
    // https://www.tistory.com/redirect
    System.out.println("URL-encoded string: " + encodedString);
    // https%3A%2F%2Fwww.tistory.com%2Fredirect
  }
}

URL Decode

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class UrlDecoderExample {
  public static void main(String[] args) throws UnsupportedEncodingException {
    String encodedString = "https%3A%2F%2Fwww.tistory.com%2Fredirect";
    String decodedString = URLDecoder.decode(encodedString, "UTF-8");
    System.out.println("URL-encoded string: " + encodedString);
    // https%3A%2F%2Fwww.tistory.com%2Fredirect
    System.out.println("URL-decoded string: " + decodedString);
    // https://www.tistory.com/redirect
  }
}

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함