fork(1) download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.net.*;
  4.  
  5. class Main
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
  10. String s;
  11. while ((s=r.readLine())!=null) {
  12. URL url = new URL(s);
  13. System.out.println("toString\t\t" + url.toString());
  14. System.out.println("getPath \t\t" + url.getPath());
  15. System.out.println("getQuery\t\t" + url.getQuery());
  16.  
  17. String path = url.getPath();
  18. String query = url.getQuery();
  19. URI uri = new URI(url.getProtocol(),
  20. null /*userInfo*/,
  21. url.getHost(),
  22. url.getPort(),
  23. (path==null)?null:URLDecoder.decode(path, "UTF-8"),
  24. (query==null)?null:URLDecoder.decode(query, "UTF-8"),
  25. null /*fragment*/);
  26.  
  27. System.out.println("uri.toString\t\t" + uri.toString());
  28.  
  29. try {
  30. System.out.println("url.toURI\t\t" + url.toURI().toString());
  31. } catch (java.net.URISyntaxException e) {
  32. System.out.println(e.toString());
  33. }
  34. System.out.println("---------------------");
  35. }
  36. }
  37. }
Success #stdin #stdout 0.07s 215872KB
stdin
http://test.com/somefile/normal.data?query=3
http://test.com/somefile/unwise|characters.data?query=3
http://test.com/somefile/escaped%20Space.data?query=7
http://test.com/somefile/unwise|escaped%20Space.data?query=something&param=4
http://test.com/somefile/unwise|noQuery.data
http://root.test.com/
http://root.test.com/?query=11
stdout
toString		http://test.com/somefile/normal.data?query=3
getPath 		/somefile/normal.data
getQuery		query=3
uri.toString		http://test.com/somefile/normal.data?query=3
url.toURI		http://test.com/somefile/normal.data?query=3
---------------------
toString		http://test.com/somefile/unwise|characters.data?query=3
getPath 		/somefile/unwise|characters.data
getQuery		query=3
uri.toString		http://test.com/somefile/unwise%7Ccharacters.data?query=3
java.net.URISyntaxException: Illegal character in path at index 31: http://test.com/somefile/unwise|characters.data?query=3
---------------------
toString		http://test.com/somefile/escaped%20Space.data?query=7
getPath 		/somefile/escaped%20Space.data
getQuery		query=7
uri.toString		http://test.com/somefile/escaped%20Space.data?query=7
url.toURI		http://test.com/somefile/escaped%20Space.data?query=7
---------------------
toString		http://test.com/somefile/unwise|escaped%20Space.data?query=something&param=4
getPath 		/somefile/unwise|escaped%20Space.data
getQuery		query=something&param=4
uri.toString		http://test.com/somefile/unwise%7Cescaped%20Space.data?query=something&param=4
java.net.URISyntaxException: Illegal character in path at index 31: http://test.com/somefile/unwise|escaped%20Space.data?query=something&param=4
---------------------
toString		http://test.com/somefile/unwise|noQuery.data
getPath 		/somefile/unwise|noQuery.data
getQuery		null
uri.toString		http://test.com/somefile/unwise%7CnoQuery.data
java.net.URISyntaxException: Illegal character in path at index 31: http://test.com/somefile/unwise|noQuery.data
---------------------
toString		http://root.test.com/
getPath 		/
getQuery		null
uri.toString		http://root.test.com/
url.toURI		http://root.test.com/
---------------------
toString		http://root.test.com/?query=11
getPath 		/
getQuery		query=11
uri.toString		http://root.test.com/?query=11
url.toURI		http://root.test.com/?query=11
---------------------