How to encode only a part of URL - java

I have incomplete URL's which I am redirecting (don't have the full URL) like
a.jsp?id=269101|14000
and
b.jsp?action=in&id=239394|2000&inmethod=
I wanted to encode the pipe "|" char only, so I started with java.net.URI class but it asks for complete url.So I used URLEncoder but it encodes the entire url.
I know I can look for | in url and encode it directly but what would be the best approach?

Using String.replace():
String myUrl = "b.jsp?action=in&id=239394|2000&inmethod=";
myUrl = myUrl.replace("|","%7C");

You need to use the URLEncoder on each query parameter value that needs to be encoded.
String url = "b.jsp?action=in" +
"&id=" + URLEncoder.encode("239394|2000", StandardCharsets.UTF_8) +
"&inmethod=";
System.out.println(url); // prints: b.jsp?action=in&id=239394%7C2000&inmethod=

Using the URLEncoder is the correct way to go. However you should do the encoding before you create your full url. using it on your full url will cause all special URL characters to be encoded. Which is not what you want here
Change your code to something like this
String url = "a.jsp?id=" + URLEncoder.encode("269101|14000",StandardCharsets.UTF_8);

Related

Java api to encode URL query parameter values?

I used URLEncoder but this seems URLEncoder does not cover everything of url query parameter encoding (escaped). I searched and someone used URLEncoder to encode whole url. For my case I iust want to encode (escape) url query parameter values.
Here are some code I am used
String url = "http://myserver.com?x=" + URLEncoder.encode(x_value, "UTF-8");
Anyone has any ideas? Thanks.

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());

Create a url which contains '&'

When i call a webservice i pass certain values in that url.
Example:
https://website.com/webserviceName/login?userName=user&password=pass
but what if the values have "&" in them.When i form such an url which contains an item with '&' the url breaks at that point returns a fault code. How do i over come this problem.
Example:
https://website.com/webserviceName/login?userName=user&user&password=pass
the problem with this url is that it breaks at the first '&'
The problem can be solved by using URLEncoder.encode(urlXml)
http://www.tutorialspoint.com/html/html_url_encoding.htm
Thanx everyone
You must encode the ampersand & with %26. So your URL will become
https://website.com/webserviceName/login?userName=user%26user&password=pass
If your username is not fixed and you want to use URLEncoder.encode as #SudhanshuUmalkar suggested, you should encode the arguments only
String url = "https://website.com/webserviceName/login?userName="
+ URLEncoder.encode(userName, "UTF-8") + "&password="
+ URLEncoder.encode(password, "UTF-8");
Since encode(String) is deprecated, you should use encode(String, "UTF-8") or whatever your character set is.
Use URLEncoder.encode() method.
url = "https://website.com/webserviceName/login?" + URLEncoder.encode("userName=user&user&password=pass", "UTF-8");
I have code working with literal ampersands in it. Your code could break because you don't provide a valid key-value parameter pair:
https://website.com/webserviceName/login?userName=user&user&password=pass
^ this shouldn't be like this
The code below works in production:
public static final String DATA_URL = "http://www.example.com/sub/folder/api.php?time=%s&lang=%s&action=test";
String.format (API.DATA_URL, "" + now, language)

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;

Escaping & in a URL

I am using jsps and in my url I have a value for a variable like say "L & T". Now when I try to retrieve the value for it by using request.getParameter I get only "L". It recognizes "&" as a separator and thus it is not getting considered as a whole string.
How do I solve this problem?
java.net.URLEncoder.encode("L & T", "utf8")
this outputs the URL-encoded, which is fine as a GET parameter:
L+%26+T
A literal ampersand in a URL should be encoded as: %26
// Your URL
http://www.example.com?a=l&t
// Encoded
http://www.example.com?a=l%26t
You need to "URL encode" the parameters to avoid this problem. The format of the URL query string is:
...?<name>=<value>&<name>=<value>&<etc>
All <name>s and <value>s need to be URL encoded, which basically means transforming all the characters that could be interpreted wrongly (like the &) into %-escaped values. See this page for more information:
http://www.w3schools.com/TAGS/ref_urlencode.asp
If you're generating the problem URL with Java, you use this method:
String str = URLEncoder.encode(input, "UTF-8");
Generating the URL elsewhere (some templates or JS or raw markup), you need to fix the problem at the source.
You can use UriUtils#encode(String source, String encoding) from Spring Web. This utility class also provides means for encoding only some parts of the URL, like UriUtils#encodePath.

Categories

Resources