Any way to send% 2b (encoded plus sign) to an arg request with java.net.URI?

I cannot find this explicitly, but it seems that you cannot send the escaped plus sign ("% 2B") as the value of the request argument if you use java.net.URI since the arg request is escaped.

// bad: http://example.com/foo?a=%252b
new URI("http", null, "example.com", 80, "/foo", "a=%2b", null);

I tried a valid "+" character, but it is sent as is, so the server will interpret it as a space.

// bad: http://example.com/foo?a=+
new URI("http", null, "example.com", 80, "/foo", "a=+", null);

So, I think you just need to do percent encoding of the keys and values ​​of the arg request and use a URI constructor with one argument that won't slip away? Perhaps let the URI go out of the “path” because the rules are complex (for example, the “+” sign means a plus sign, not a space, when on the way):

// good: http://example.com/foo?a=%2b
new URI(new URI("http", null, "example.com", 80, "/foo", null, null).toASCIIString() + "?a=%2b");

, , URI, URI:

URI u = ...;
URI identical = new URI(u.getScheme(),
        u.getUserInfo(),
        u.getPath(), u.getQuery(),
        u.getFragment());

, % 2b

URI u = new URI("http://example.com:80/foo?a=%2b");
URI identical = ...; // not identical! http://example.com:80/foo?a=+

, , apache commons spring ?

PS: http://docs.oracle.com/javase/6/docs/api/java/net/URI.html URI, " ". "".

+5
3

. , % 2b , URI- . :

URI uri = new URI(...);
String asciiUrl = uri.toASCIIString();
String plusReplaced = asciiUrl.replace("+", "%2b");
+3

, Spring 5 RestTemplate Apache HttpClient . , %2B (...) REST API Couchbase.

API , + URL-, %2B .

, ProtocolExec HttpClient URI , %2B.

  • + +.
  • %2B +
  • %252B ( %2B %) %252B

, MainExec, , , HttpClientBuilder MainExec, ProtocolExec. :

/*
 * This is a hack to circumvent Apache HttpClient otherwise inevitable URI rewriting. This rewriting made it
 * impossible to send a literal %2B in a query path as it includes a forceful reencoding that doesn't reencode the
 * '+' character. This is necessary because Couchbase decided that they were too good for following standards and
 * decided to use + as meaning " " (whitespace) in URI paths instead of the typical %20.
 *
 * As an ugly solution we wrote an HttpClientBuilder that injects a ClientExecChain that will rewrite the full path
 * turning + to spaces. Maybe future versions will make this easier to accomplish.
 */
public class CustomHttpClientBuilder extends HttpClientBuilder {

    @Override
    protected ClientExecChain decorateMainExec(final ClientExecChain requestExecutor) {
        return (
                HttpRoute route,
                HttpRequestWrapper request,
                HttpClientContext clientContext,
                HttpExecutionAware execAware) -> {
            UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUri(request.getURI());
            uriComponentsBuilder.replacePath(request.getURI().getRawPath().replace("+", "%2B"));
            request.setURI(uriComponentsBuilder.encode().build(true).toUri());

            return requestExecutor.execute(route, request, clientContext, execAware);
        };
    }
}

Spring UriComponentsBuilder URI, UriComponentsBuilder java.net.URI.

, , . .

, - - .

0
-2

All Articles