language: Java7 (sun-jdk-1.7.0_10)
date: 160 days 0 hours ago
link:
visibility: public
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
32
33
34
35
36
37
import java.util.*;
import java.lang.*;
import java.net.*;
 
class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
        java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
        String s;
        while ((s=r.readLine())!=null) {
            URL url = new URL(s);
            System.out.println("toString\t\t" + url.toString());
            System.out.println("getPath \t\t" + url.getPath());
            System.out.println("getQuery\t\t" + url.getQuery());
 
            String path  = url.getPath();
            String query = url.getQuery();
            URI uri = new URI(url.getProtocol(), 
                              null /*userInfo*/,
                              url.getHost(), 
                              url.getPort(), 
                              (path==null)?null:URLDecoder.decode(path, "UTF-8"), 
                              (query==null)?null:URLDecoder.decode(query, "UTF-8"), 
                              null /*fragment*/);
 
            System.out.println("uri.toString\t\t" + uri.toString());
 
            try {
                System.out.println("url.toURI\t\t" + url.toURI().toString());
            } catch (java.net.URISyntaxException e) {
                System.out.println(e.toString());
            }
            System.out.println("---------------------");
        }
    }
}
  • upload with new input
  • result: Success     time: 0.07s    memory: 215872 kB     returned value: 0

    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
    
    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
    ---------------------