why apache-commons lib encoding space as + not as %20?
Asked Answered
B

3

8

i'm using URLCodec from Apache Commons Codec to encode URL, but it encode space as + NOT as %20

why? and what is the solution?

Basketry answered 21/2, 2011 at 21:12 Comment(1)
related #2679051Cuttle
C
5

See this related question

Of course, you can always do url.replace("+", "%20"); if you need it (after encoding)

Cuttle answered 21/2, 2011 at 21:19 Comment(4)
@Johan: I deleted my comment before seeing yours, because I felt like I was sniping at Bozho, and that wasn't my intent. w3schools isn't very reliable. For instance, to pick a random example, if you relied on their description of the JavaScript String#replace function (w3schools.com/jsref/jsref_replace.asp), you wouldn't know that you can supply a function for the second parameter, which is really useful.Butz
@T.J. Crowder anyway thanks for pointing the unreliability of w3schools. I removed the quote anyway, it is not that much relevant anyway.Cuttle
"Of course, you can always do url.replace(" ", "%20");"... That would be a bad idea (you don't want to do this before encoding, you'll end up with an encoded %; and the space isn't there after encoding). You'd want .encode(str).replace("+", "%20");.Butz
@T.J. Crowder yup, you are right, I meant after the encoding, but forgot that it's turned into a plus :)Cuttle
E
2

Because + is an equally valid way of encoding a space. What are you trying to "solve"?

Erysipelas answered 21/2, 2011 at 21:15 Comment(4)
dear, i need that .. where i have web service that understand %20 and not + .. check both of these plz aydeena.com/Services/Search.svc/JSON/SearchByText/… aydeena.com/Services/Search.svc/JSON/SearchByText/…Basketry
@adham: In that case, that web service is not adhering to the standard. It should be fixed.Erysipelas
@adham: if your webservice can't decode a + as a space, it is broken and not the encoding. If the root cause, the non-conformant webservice, I would bet that some manual string processing is going on, and god forbid a regex, instead of using a decoding function designed to conform to the specification.Ectomy
You're not supposed to decode + as a space in an URL for other than the query stringForereach
F
2

The URLCodec encodes stuff suitable a submitted form, which is not the same as percent encoding a URL. There's more explanation in this question

See this question for how you should encode your URL.

Forereach answered 21/2, 2011 at 21:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.