java.net.URI resolve against only query string

前端 未结 2 646
心在旅途
心在旅途 2021-01-18 10:35

I\'m trying to build URI\'s using the JDK java.net.URI.
I want to append to an absolute URI object, a query (in String). In example:



        
2条回答
  •  抹茶落季
    2021-01-18 11:18

    First Case:

    base = http://example.com/something/more/long
    

    and query evaluates to be

    query = ?query=http://local:282/rand&action=aaaa.
    

    According to the documentation of method public URI resolve(URI uri) , it resolves the query URI against the base URI. While resolving if the method finds a path in the query URI, it assigns the same path to the new resolved URI. In this case there is no path associated with the query. If you see the below snippet of the resolve() function, it will get more clear.

    //snippet

     String cp = (child.path == null) ? "" : child.path;
            if ((cp.length() > 0) && (cp.charAt(0) == '/')) {
            // 5.2 (5): Child path is absolute
            ru.path = child.path;
            } else {
            // 5.2 (6): Resolve relative path
            ru.path = resolvePath(base.path, cp, base.isAbsolute());
            }
    

    where cp is the child(in your case query path). As its null here, the flow goes into the else loop where the resolved query is a assigned a path from the base URI.

    Your new URI has this path /something/more/, as it strips everything after the last "/" character.

    Second Case:

    base = http://example.com/something/more/long and query evaluates to be

    query = /something/more/long?query=http://local:282/rand&action=aaaa
    

    Here it goes to the if loop which assigns the query path to the new URI's path. Path of your query URI is /something/more/long here i.e it includes the "long" value as well. May be this is how they resolve the URI's. Look through the documentation you will have a more clear idea of it.

提交回复
热议问题