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

adarshr
  • 61,315
  • 23
  • 138
  • 167
SUM
  • 1,651
  • 4
  • 33
  • 57

4 Answers4

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");
jtahlborn
  • 52,909
  • 5
  • 76
  • 118
  • Possibly overkill in this situation, but a good approach for the general case. URLEncoder does HTML form encoding, which most servers handle by default. – Alex Jun 08 '12 at 14:26
  • Did try this. It encodes it as (102+OR+140) But I want it as (102%20OR%20140) – SUM Jun 08 '12 at 17:18
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

Giovanni Caporaletti
  • 5,426
  • 2
  • 26
  • 39
  • 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. – Alex Jun 08 '12 at 14:19
  • 1
    you don't want to use URI for the encoding, you want to use URLEncoder. – jtahlborn Jun 08 '12 at 14:20
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());
Community
  • 1
  • 1
Alex
  • 13,811
  • 1
  • 37
  • 50
  • no, you want to encode the query params before adding them to the url. URI will not handle this correectly for you. – jtahlborn Jun 08 '12 at 14:26
  • @jtahlborn What's incorrect about it? It produces exactly the desired output according to the original question. – Alex Jun 08 '12 at 14:29
  • what happens if your query param is "(A=B)"? – jtahlborn Jun 08 '12 at 14:30
  • @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. – Alex Jun 08 '12 at 14:38
  • if you care enough to escape your params (assuming they may have some invalid values), you might as well do it the right way... – jtahlborn Jun 08 '12 at 14:43
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.

Community
  • 1
  • 1
Tooraj Jam
  • 1,592
  • 14
  • 27
  • 1
    So another solution is [Apache Commons Codec](http://commons.apache.org/codec/apidocs/org/apache/commons/codec/net/URLCodec.html) – Tooraj Jam Jun 08 '12 at 14:40