2

I use uribuilder object from apache.http.client to create url

example:www.xxx.com/#/path/?query=123

my code as follow

URIBuilder uriBuilder = new URIBuilder();
uriBuilder.setScheme("http");
uriBuilder.setHost(host);
uriBuilder.setFragment(path);
uriBuilder.addParameter(query,123);

but the result is www.xxx.com/?query=123#path, how can I get correct url as I expected by uribuilder or other java tool library.

1 Answers1

3

A valid URI needs to comply with the following structure:

scheme:[//[user:password@]host[:port]][/]path[?query][#fragment]

The URI you are trying to create looks like a URI used in a single page application. In that case, the query part is a part of the fragment.

You can create it like this:

URIBuilder uriBuilder = new URIBuilder();
uriBuilder.setScheme("http");
uriBuilder.setHost("www.xxx.com");
uriBuilder.setPath("/");
uriBuilder.setFragment("/path/?query=123");
URI uri = uriBuilder.build();
nille85
  • 301
  • 2
  • 4