Encode URL parameters using Java - java

I want to encode part of URL paramter in JAVA
http://statebuild-dev.com/iit-title/size.xml?id=(102T OR 140T)
to
http://statebuild-dev.com/iit-title/size.xml?id=(102%20OR%20140)
have tried using URI to encode but it also encodes ? which I do not want. In the URL I want to encode the part after '='
URI uri = new URI("http",
"statebuild-dev.com/iit-title", "/size.xml?id=(102 OR 140)", null);
//URL url = uri.toURL();
System.out.println(uri.toString());
System.out.println(url1);
Thank You

you want to use URLEncoder to encode each query parameter before adding to the url, e.g.:
String encodedValue = URLEncoder.encode("(102 OR 140)", "UTF-8");

This answer has a good discussion of encoding the various parts of a URI/URL. You're on the right track, but your specific problem is that you have the various parts of the URI wrong. You need to use the multi-part constructor that takes an authority, path, query, and fragment:
URI uri = new URI("http", "statebuild-dev.com", "/iit-title/size.xml", "id=(102 or 104)", null);
System.out.println(uri.toString());
System.out.println(uri.toASCIIString());

You used the wrong constructor.
Try this:
URI uri = new URI("http","statebuild-dev.com", "/iit-title/size.xml", "id=(102 OR 140)", null);
See also java.net.URLEncoder

The java.net.URI class can help; in the documentation of URL you find
Note, the URI class does perform escaping of its component fields in certain circumstances. The recommended way to manage the encoding and decoding of URLs is to use URI
Check this thread.

Related

How to create URI/URL using MultiValueMap for parameters?

I have multimap of parameters, like this:
{
keyA: ["2+4", "4+8"],
keyB: ["Some words with special chars #ąęć"]
}
as spring MultiValueMap and I'm trying to create URI from this, I tried to use
URI uri = UriComponentsBuilder
.fromUriString(baseUri).path(somePath)
.queryParams(params.getQueryParameters())
.build().encode().toUri();
It seems to work for special chars, but it still does think that + sign is a space, I want to encode all parameters, is there existing solution for this other than manually encoding each value?
Assuming you are on Spring 5.0, this is discussed in detail in [SPR-16860] Spring is inconsistent in the encoding/decoding of URLs issue. More or less it boils down to this:
From an RFC 3986 perspecitve, "+" is a legal character. By default the RestTemplate leaves it as is.
UriComponents.encode() will leave + sign as is, to stay complaint with the RFC 3986. If you need it encoded one suggestion is to use UriUtils:
String value = "A+B=C";
value = UriUtils.encode(value, StandardCharsets.UTF_8); // A%2BB%3DC
URI uri = UriComponentsBuilder.newInstance()
.queryParam("test", value)
.build(true)
.toUri();
There is a change coming 5.0.8 as part of [SPR-17039] Support stricter encoding of URI variables in UriComponents which introduces new UriComponentsBuilder.encode() method. It should be enough to move encode() before build() in your example:
URI uri = UriComponentsBuilder
.fromUriString(baseUri).path(somePath)
.queryParams(params.getQueryParameters())
.encode()
.build()
.toUri();

How to let UriBuilder build an URI with decoded hash character?

I try to generate following uri
//host:port/name/#/name/name
with jersey URI Builder. The Problem is that the uri builder decode the hash character.
Does anyone how to build the given URI?
Thats what I have:
final URI build = uriInfo.getBaseUriBuilder().path("..").path("#").path("/clients/asd/").build();
# is not a valid character in a URI path; # and what follows it is actually a URI fragment.
What you need to do is therefore:
final URI build = uriInfo.getBaseUriBuilder().path("..")
.fragment("/clients/asd/").build();
(note: I don't use Jersey, actually; I looked up the javadoc here, as I suppose this is the same)
For me, this is what worked:
final URI build = UriBuilder.fromUri("{arg1}").fragment("{arg2}").buildFromEncoded("../","/clients/asd/");
Using .path("..").fragment("/clients/asd/").build() would generate
..#%2Fclients%2Fasd

In Java, how to get canonicalized url

Say i have space in a url, what is the right way to convert it to %20? no 'replace' suggestion please.
For example, if you put "http://test.com/test and test/a" into the browser window, it converts to http://test.com/test%20and%20test/a
If I use URLEncoder, I get even the / converted. which is not what i want.
Thanks,
this is the right way, seems like. to add to the question, what if there is also some non ascii code in the path that I want convert to valid url with utf8 encode? e.g.: test.com:8080/test and test/pierlag2_carré/a?query=世界 I'd want it to be converted to test.com:8080/test%20and%20test/pierlag2_carr%C3%A9/a?query=%E4%B8%96%E7%95%8C
Try splitting into a URI with the aid of the URL class:
String sUrl = "http://test.com:8080/test and test/a?query=world";
URL url = new URL(sUrl);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
String canonical = uri.toString();
System.out.println(canonical);
Output:
http://test.com:8080/test%20and%20test/a?query=world
The correct way to build URLs in Java is to create a URI object and fill out each part of the URL. The URI class handles the encoding rules for the distinct parts of the URL as they differ from one to the next.
URLEncoder is not what you want, despite its name, as that actually does HTML form encoding.
EDIT:
Based on your comments, you are receiving the URL as input to your application and do not control the initial generation of the URL. The real problem you are currently experiencing is that the input you are receiving, the URL, is not a valid URL. URLs / URIs cannot contain spaces per the spec (hence the %20 in the browser).
Since you have no control over the invalid input you are going to be forced to split the incoming URL into its parts:
scheme
host
path
Then you are going to have to split the path and separately encode each part to ensure that you do not inadvertently encode the / that delimits your path fragments.
Finally, you can put all of them back together in a URI object and then pass that around your application.
You may find useful this code to replace blank spaces in your URL:
String myUrl = "http://test.com/test and test/a";
myUrl = myUrl.replaceAll(" ", "%20");
URI url = new URI(myUrl);
System.out.print(url.toString());

How to convert URL toURI when there are unwise characters?

I've got URL object with path containing unwise characters (RFC 2396) in my case it is "|" (pipe) character.
Now I need to safely convert that to URI, but URL.toURI() throws an exception.
I've read URL documentation but this part is for me confusing:
The URL class does not itself encode or decode any URL components
according to the escaping mechanism defined in RFC2396. It is the
responsibility of the caller to encode any fields, which need to be
escaped prior to calling URL, and also to decode any escaped fields,
that are returned from URL. Furthermore, because URL has no knowledge
of URL escaping, it does not recognize equivalence between the encoded
or decoded form of the same URL.
So how should I do it? What is the pattern here to encode this characters during conversion? Do I need create encoded copy of my URL object?
OK, I come up with something like this:
URI uri = new URI(url.getProtocol(),
null /*userInfo*/,
url.getHost(),
url.getPort(),
(url.getPath()==null)?null:URLDecoder.decode(url.getPath(), "UTF-8"),
(url.getQuery()==null)?null:URLDecoder.decode(url.getQuery(), "UTF-8"),
null /*fragment*/);
Looks like it works, here is an example. Can some one confirm that this is proper solution?
Edit: initial solution had some problems when there was a query so I've fixed it.
Use URL encoding?
From your example, you currently have:
URL url = new URL("http", "google.com", 8080, "/crapy|path with-unwise_characters.jpg");
Instead, I would use:
String path = "/crapy|path with-unwise_characters.jpg"
URL url = new URL("http", "google.com", 8080, URLEncoder.encode(path, "UTF-8"));
This should work and handle all unwise characters in the path as per the standard URL encoding.
HTTPClient 4 has an object for that org.apache.http.client.utils.URIBuilder:
URIBuilder builder =
new URIBuilder()
.setScheme(url.getProtocol())
.setHost(url.getHost())
.setPort(url.getPort())
.setUserInfo(url.getUserInfo())
.setPath(url.getPath())
.setQuery(url.getQuery());
URI uri = builder.build();
return uri;

Invoking Http Request In Java

I have standalone, swing based application, that allows the user to enter any URL and it returns the status code.
I want to let the user to enter any URL that works when he uses the same URL in a browser, no matter what the URL is (e.g. parameters with special characters, json strings, etc.).
How can I implement that?
I tried to use URL class, but in some cases, I saw that one web site did not accept a json string I gave, although it was accepted when I copy the URL to the browser.
You might want to look at the java.net.URI object. Some of the constructors will properly escape extended characters.
URI(String scheme,
String authority,
String path,
String query,
String fragment)
Even so, you'll need to carefully encode the query string to make sure the JSON doesn't spill over into other parameters if there are & characters, etc.
Finally, this worked for me:
String urlStr = "http://abc.dev.domain.com/0007AC/ads/800x480 15sec h.264.mp4";
URL url = new URL(urlStr);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
url = uri.toURL();

Categories

Resources