Java - Write code to do url encode

Requirements

Write code to url encode

Use UTF-8 as encoding

Hint

Use URLEncoder.encode() method

Demo

//package com.book2s;
import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;

public class Main {
    public static void main(String[] argv) {
        String str = "book2s.com";
        System.out.println(urlencode(str));
    }/*  www  .  j  a  va2  s  .c om*/

    public static String urlencode(String str) {
        try {
            return URLEncoder.encode(str, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new IllegalArgumentException(e);
        }
    }
}