fork(2) 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://t...content-available-to-author-only...t.com/somefile/normal.data?query=3
http://t...content-available-to-author-only...t.com/somefile/unwise|characters.data?query=3
http://t...content-available-to-author-only...t.com/somefile/escaped%20Space.data?query=7
http://t...content-available-to-author-only...t.com/somefile/unwise|escaped%20Space.data?query=something&param=4
http://t...content-available-to-author-only...t.com/somefile/unwise|noQuery.data
http://r...content-available-to-author-only...t.com/
http://r...content-available-to-author-only...t.com/?query=11
stdout
toString		http://t...content-available-to-author-only...t.com/somefile/normal.data?query=3
getPath 		/somefile/normal.data
getQuery		query=3
uri.toString		http://t...content-available-to-author-only...t.com/somefile/normal.data?query=3
url.toURI		http://t...content-available-to-author-only...t.com/somefile/normal.data?query=3
---------------------
toString		http://t...content-available-to-author-only...t.com/somefile/unwise|characters.data?query=3
getPath 		/somefile/unwise|characters.data
getQuery		query=3
uri.toString		http://t...content-available-to-author-only...t.com/somefile/unwise%7Ccharacters.data?query=3
java.net.URISyntaxException: Illegal character in path at index 31: http://t...content-available-to-author-only...t.com/somefile/unwise|characters.data?query=3
---------------------
toString		http://t...content-available-to-author-only...t.com/somefile/escaped%20Space.data?query=7
getPath 		/somefile/escaped%20Space.data
getQuery		query=7
uri.toString		http://t...content-available-to-author-only...t.com/somefile/escaped%20Space.data?query=7
url.toURI		http://t...content-available-to-author-only...t.com/somefile/escaped%20Space.data?query=7
---------------------
toString		http://t...content-available-to-author-only...t.com/somefile/unwise|escaped%20Space.data?query=something&param=4
getPath 		/somefile/unwise|escaped%20Space.data
getQuery		query=something&param=4
uri.toString		http://t...content-available-to-author-only...t.com/somefile/unwise%7Cescaped%20Space.data?query=something&param=4
java.net.URISyntaxException: Illegal character in path at index 31: http://t...content-available-to-author-only...t.com/somefile/unwise|escaped%20Space.data?query=something&param=4
---------------------
toString		http://t...content-available-to-author-only...t.com/somefile/unwise|noQuery.data
getPath 		/somefile/unwise|noQuery.data
getQuery		null
uri.toString		http://t...content-available-to-author-only...t.com/somefile/unwise%7CnoQuery.data
java.net.URISyntaxException: Illegal character in path at index 31: http://t...content-available-to-author-only...t.com/somefile/unwise|noQuery.data
---------------------
toString		http://r...content-available-to-author-only...t.com/
getPath 		/
getQuery		null
uri.toString		http://r...content-available-to-author-only...t.com/
url.toURI		http://r...content-available-to-author-only...t.com/
---------------------
toString		http://r...content-available-to-author-only...t.com/?query=11
getPath 		/
getQuery		query=11
uri.toString		http://r...content-available-to-author-only...t.com/?query=11
url.toURI		http://r...content-available-to-author-only...t.com/?query=11
---------------------