I always forget and struggling about the behavior of Java methods and classes for encoding uri.
To clear my heads I will post small test codes.
You might also want to check:
import java.net.URI;
import java.net.URLEncoder;
import java.util.BitSet;
import org.apache.commons.httpclient.util.URIUtil;
public class URIEncodeExample {
// hehe using Japanese :p
private static final String INPUT = "あいうえお/";
public static void main(String[] args) throws Exception {
// java.net.URI
// slash is not encoded because it is detected as a path separator
// %E3%81%82%E3%81%84%E3%81%86%E3%81%88%E3%81%8A/
System.out.println(URI.create(INPUT).toASCIIString());
// java.net.URI + relativize
// slash is not encoded because it is detected as a path separator
// %E3%81%82%E3%81%84%E3%81%86%E3%81%88%E3%81%8A/
URI root = new URI("http", "dummy.com", null, null);
URI uri = new URI("http", "dummy.com", "/"+INPUT, null);
System.out.println(root.relativize(uri).toASCIIString());
// java.net.URLEncoder
// encode entire string including slash
// %E3%81%82%E3%81%84%E3%81%86%E3%81%88%E3%81%8A%2F
System.out.println(URLEncoder.encode(INPUT, "utf-8"));
// org.apache.commons.httpclient.util.URIUtil
// %E3%81%82%E3%81%84%E3%81%86%E3%81%88%E3%81%8A%2F
System.out.println(URIUtil.encode(INPUT, new BitSet()));
// org.apache.commons.httpclient.util.URIUtil
// you can specify which is already encoded character by BitSet
// here slash is skipped to be encoded
// %E3%81%82%E3%81%84%E3%81%86%E3%81%88%E3%81%8A/
BitSet allowed = new BitSet();
allowed.set('/', true);
System.out.println(URIUtil.encode(INPUT, allowed));
}
}
You might also want to check:
コメント