2

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

4 Answers 4

3

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");
Sign up to request clarification or add additional context in comments.

2 Comments

Possibly overkill in this situation, but a good approach for the general case. URLEncoder does HTML form encoding, which most servers handle by default.
Did try this. It encodes it as (102+OR+140) But I want it as (102%20OR%20140)
1

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

5 Comments

no, you want to encode the query params before adding them to the url. URI will not handle this correectly for you.
@jtahlborn What's incorrect about it? It produces exactly the desired output according to the original question.
what happens if your query param is "(A=B)"?
@jtahlborn OK, point taken. That MAY cause a problem in some applications. You're assuming that the query is to be interpreted as HTML form parameters. The URI constructor will just treat it as an opaque string, which might be good enough. But you should still be using the multi-part URI constructor; using URLEncoder on the query params is an optional extra step.
if you care enough to escape your params (assuming they may have some invalid values), you might as well do it the right way...
1

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

2 Comments

This is wrong, it produces "http:/statebuild-dev.com/iit-title/size.xml?id=(102%20or%20104)". statebuild-dev.com is the authority, not part of the path.
you don't want to use URI for the encoding, you want to use URLEncoder.
0

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.

1 Comment

So another solution is Apache Commons Codec

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.