You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by MF <li...@yahoo.it> on 2013/02/12 10:08:45 UTC
encode querystring value
Hi all,
I have to encode querystring value in a http request in Java. For example:
http://mysite.com/service?name=James Smith&address=my street
must become:
http://mysite.com/service?name=James%20Smith&address=my%20street
I have to do this in a method where I have querystring value in input, so I have to encode only the value (James Smith, my street, in my example). I was thinking about using this method org.apache.commons.httpclient.util.URIUtil.encodeWithinQuery: is this right? I have seen many confusing discussion about encoding http request in many other forum.
Thanks for answering.
Re: encode querystring value
Posted by François-Xavier Bonnet <fr...@centraliens.net>.
There is no difference between java.net.URLEncoder and commons-codec.
On 12/02/2013 11:44, listeaddr@yahoo.it wrote:
> I've test it and it seems to work. Could I use also
> java.net.URLEncoder? Is there any difference?
> Thanks again.
>
>
> ------------------------------------------------------------------------
> *Da:* François-Xavier Bonnet <fr...@centraliens.net>
> *A:* MF <li...@yahoo.it>
> *Cc:* HttpClient User Discussion <ht...@hc.apache.org>
> *Inviato:* Martedì 12 Febbraio 2013 11:02
> *Oggetto:* Re: encode querystring value
>
> "+" is the standard way to encode space character. Your site should
> support it.
> You can also use class java.net.URLEncoder included in the JDK but
> spaces will still be encoded as "+".
> Why would it ba a problem for you?
>
> On 12/02/2013 10:50, listeaddr@yahoo.it <ma...@yahoo.it> wrote:
> > Thanks for answering. But URLCodec and URLEncodedUtils both replace
> space with "+" and not with "%20".
> >
> > Are there any know problems in using URIUtil? I'm not using HttpClient.
> >
> >
> > ------------------------------------------------------------------------
> > *Da:* François-Xavier Bonnet <francois-xavier.bonnet@centraliens.net
> <ma...@centraliens.net>>
> > *A:* HttpClient User Discussion <httpclient-users@hc.apache.org
> <ma...@hc.apache.org>>
> > *Cc:* MF <listeaddr@yahoo.it <ma...@yahoo.it>>
> > *Inviato:* Martedì 12 Febbraio 2013 10:25
> > *Oggetto:* Re: encode querystring value
> >
> > Hi,
> >
> > This method is from HttpClient 3 which is now deprecated and not
> maintained anymore. In the latest versions you can use class
> org.apache.http.client.utils.URLEncodedUtils
> > I guess you are already using HttpClient on your project? If not, if
> you just need to encode values for querystring, you can use
> commons-codec library:
> >
> http://commons.apache.org/codec/apidocs/org/apache/commons/codec/net/URLCodec.html#encode(java.lang.String,
> java.lang.String)
> <http://commons.apache.org/codec/apidocs/org/apache/commons/codec/net/URLCodec.html#encode%28java.lang.String,%20java.lang.String%29>
> >
> > Regards,
> > Francois-Xavier
> >
> > On 12/02/2013 10:08, MF wrote:
> > > Hi all,
> > > I have to encode querystring value in a http request in Java. For
> example:
> > > http://mysite.com/service?name=James Smith&address=my street
> > > must become:
> > > http://mysite.com/service?name=James%20Smith&address=my%20street
> > > I have to do this in a method where I have querystring value in
> input, so I have to encode only the value (James Smith, my street, in
> my example). I was thinking about using this method
> org.apache.commons.httpclient.util.URIUtil.encodeWithinQuery: is this
> right? I have seen many confusing discussion about encoding http
> request in many other forum.
> > > Thanks for answering.
> >
> >
> >
>
>
>
Re: encode querystring value
Posted by MF <li...@yahoo.it>.
I've test it and it seems to work. Could I use also java.net.URLEncoder? Is there any difference?
Thanks again.
________________________________
Da: François-Xavier Bonnet <fr...@centraliens.net>
A: MF <li...@yahoo.it>
Cc: HttpClient User Discussion <ht...@hc.apache.org>
Inviato: Martedì 12 Febbraio 2013 11:02
Oggetto: Re: encode querystring value
"+" is the standard way to encode space character. Your site should support it.
You can also use class java.net.URLEncoder included in the JDK but spaces will still be encoded as "+".
Why would it ba a problem for you?
On 12/02/2013 10:50, listeaddr@yahoo.it wrote:
> Thanks for answering. But URLCodec and URLEncodedUtils both replace space with "+" and not with "%20".
>
> Are there any know problems in using URIUtil? I'm not using HttpClient.
>
>
> ------------------------------------------------------------------------
> *Da:* François-Xavier Bonnet <fr...@centraliens.net>
> *A:* HttpClient User Discussion <ht...@hc.apache.org>
> *Cc:* MF <li...@yahoo.it>
> *Inviato:* Martedì 12 Febbraio 2013 10:25
> *Oggetto:* Re: encode querystring value
>
> Hi,
>
> This method is from HttpClient 3 which is now deprecated and not maintained anymore. In the latest versions you can use class org.apache.http.client.utils.URLEncodedUtils
> I guess you are already using HttpClient on your project? If not, if you just need to encode values for querystring, you can use commons-codec library:
> http://commons.apache.org/codec/apidocs/org/apache/commons/codec/net/URLCodec.html#encode(java.lang.String, java.lang.String) <http://commons.apache.org/codec/apidocs/org/apache/commons/codec/net/URLCodec.html#encode%28java.lang.String,%20java.lang.String%29>
>
> Regards,
> Francois-Xavier
>
> On 12/02/2013 10:08, MF wrote:
> > Hi all,
> > I have to encode querystring value in a http request in Java. For example:
> > http://mysite.com/service?name=James Smith&address=my street
> > must become:
> > http://mysite.com/service?name=James%20Smith&address=my%20street
> > I have to do this in a method where I have querystring value in input, so I have to encode only the value (James Smith, my street, in my example). I was thinking about using this method org.apache.commons.httpclient.util.URIUtil.encodeWithinQuery: is this right? I have seen many confusing discussion about encoding http request in many other forum.
> > Thanks for answering.
>
>
>
Re: encode querystring value
Posted by François-Xavier Bonnet <fr...@centraliens.net>.
"+" is the standard way to encode space character. Your site should
support it.
You can also use class java.net.URLEncoder included in the JDK but
spaces will still be encoded as "+".
Why would it ba a problem for you?
On 12/02/2013 10:50, listeaddr@yahoo.it wrote:
> Thanks for answering. But URLCodec and URLEncodedUtils both replace
> space with "+" and not with "%20".
>
> Are there any know problems in using URIUtil? I'm not using HttpClient.
>
>
> ------------------------------------------------------------------------
> *Da:* François-Xavier Bonnet <fr...@centraliens.net>
> *A:* HttpClient User Discussion <ht...@hc.apache.org>
> *Cc:* MF <li...@yahoo.it>
> *Inviato:* Martedì 12 Febbraio 2013 10:25
> *Oggetto:* Re: encode querystring value
>
> Hi,
>
> This method is from HttpClient 3 which is now deprecated and not
> maintained anymore. In the latest versions you can use class
> org.apache.http.client.utils.URLEncodedUtils
> I guess you are already using HttpClient on your project? If not, if
> you just need to encode values for querystring, you can use
> commons-codec library:
> http://commons.apache.org/codec/apidocs/org/apache/commons/codec/net/URLCodec.html#encode(java.lang.String,
> java.lang.String)
> <http://commons.apache.org/codec/apidocs/org/apache/commons/codec/net/URLCodec.html#encode%28java.lang.String,%20java.lang.String%29>
>
> Regards,
> Francois-Xavier
>
> On 12/02/2013 10:08, MF wrote:
> > Hi all,
> > I have to encode querystring value in a http request in Java. For
> example:
> > http://mysite.com/service?name=James Smith&address=my street
> > must become:
> > http://mysite.com/service?name=James%20Smith&address=my%20street
> > I have to do this in a method where I have querystring value in
> input, so I have to encode only the value (James Smith, my street, in
> my example). I was thinking about using this method
> org.apache.commons.httpclient.util.URIUtil.encodeWithinQuery: is this
> right? I have seen many confusing discussion about encoding http
> request in many other forum.
> > Thanks for answering.
>
>
>
Re: encode querystring value
Posted by MF <li...@yahoo.it>.
Thanks for answering. But URLCodec and URLEncodedUtils both replace space with "+" and not with "%20".
Are there any know problems in using URIUtil? I'm not using HttpClient.
________________________________
Da: François-Xavier Bonnet <fr...@centraliens.net>
A: HttpClient User Discussion <ht...@hc.apache.org>
Cc: MF <li...@yahoo.it>
Inviato: Martedì 12 Febbraio 2013 10:25
Oggetto: Re: encode querystring value
Hi,
This method is from HttpClient 3 which is now deprecated and not maintained anymore. In the latest versions you can use class org.apache.http.client.utils.URLEncodedUtils
I guess you are already using HttpClient on your project? If not, if you just need to encode values for querystring, you can use commons-codec library:
http://commons.apache.org/codec/apidocs/org/apache/commons/codec/net/URLCodec.html#encode(java.lang.String, java.lang.String) <http://commons.apache.org/codec/apidocs/org/apache/commons/codec/net/URLCodec.html#encode%28java.lang.String,%20java.lang.String%29>
Regards,
Francois-Xavier
On 12/02/2013 10:08, MF wrote:
> Hi all,
> I have to encode querystring value in a http request in Java. For example:
> http://mysite.com/service?name=James Smith&address=my street
> must become:
> http://mysite.com/service?name=James%20Smith&address=my%20street
> I have to do this in a method where I have querystring value in input, so I have to encode only the value (James Smith, my street, in my example). I was thinking about using this method org.apache.commons.httpclient.util.URIUtil.encodeWithinQuery: is this right? I have seen many confusing discussion about encoding http request in many other forum.
> Thanks for answering.
Re: encode querystring value
Posted by François-Xavier Bonnet <fr...@centraliens.net>.
Hi,
This method is from HttpClient 3 which is now deprecated and not
maintained anymore. In the latest versions you can use class
org.apache.http.client.utils.URLEncodedUtils
I guess you are already using HttpClient on your project? If not, if you
just need to encode values for querystring, you can use commons-codec
library:
http://commons.apache.org/codec/apidocs/org/apache/commons/codec/net/URLCodec.html#encode(java.lang.String,
java.lang.String)
<http://commons.apache.org/codec/apidocs/org/apache/commons/codec/net/URLCodec.html#encode%28java.lang.String,%20java.lang.String%29>
Regards,
Francois-Xavier
On 12/02/2013 10:08, MF wrote:
> Hi all,
> I have to encode querystring value in a http request in Java. For example:
> http://mysite.com/service?name=James Smith&address=my street
> must become:
> http://mysite.com/service?name=James%20Smith&address=my%20street
> I have to do this in a method where I have querystring value in input, so I have to encode only the value (James Smith, my street, in my example). I was thinking about using this method org.apache.commons.httpclient.util.URIUtil.encodeWithinQuery: is this right? I have seen many confusing discussion about encoding http request in many other forum.
> Thanks for answering.