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 vb...@scdi.org on 2008/06/21 10:01:40 UTC
utility class to create URL strings
All,
I feel kind of stupid to ask such a simple question but I couldn't
find the right library to do this simple job (and even asking around
didn't return much valuable information).
I would like to provide a handy way to (only) create a string URL
definition, manage the parameters and handle the encoding. Something
like:
URLDefinition def = new URLDefinition("http://google.com");
def.addParameter("lang", "de");
def.addParameter("country", "ch");
String a = def.toString();
and a is magically "http://google.com?lang=de&country=ch" :-)
I haven't seen anything ready-to-use in the various commons libraries
(HttpParams is the closest I could find). Is there anything out there
that I've been missing or should I simply roll my own version?
Best regards,
-Vladimir
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org
Re: utility class to create URL strings
Posted by Quintin Beukes <qu...@last.za.net>.
Hey,
What is wrong with java.net.URL?
URL u = new URL("http://www.google.com");
u.getHost();
u.getFile();
u.getPath();
u.getProtocol();
u.getQuery();
u.getRef();
Not sure you can check the encoding, but it would probably match the
encoding used by all your string objects.
Q
On 6/21/08, vbossica@scdi.org <vb...@scdi.org> wrote:
>
> All,
>
> I feel kind of stupid to ask such a simple question but I couldn't find the
> right library to do this simple job (and even asking around didn't return
> much valuable information).
>
> I would like to provide a handy way to (only) create a string URL
> definition, manage the parameters and handle the encoding. Something like:
>
> URLDefinition def = new URLDefinition("http://google.com");
> def.addParameter("lang", "de");
> def.addParameter("country", "ch");
> String a = def.toString();
>
> and a is magically "http://google.com?lang=de&country=ch" :-)
>
> I haven't seen anything ready-to-use in the various commons libraries
> (HttpParams is the closest I could find). Is there anything out there that
> I've been missing or should I simply roll my own version?
>
> Best regards,
>
> -Vladimir
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>
--
Quintin Beukes
Re: utility class to create URL strings
Posted by Oleg Kalnichevski <ol...@apache.org>.
On Sat, 2008-06-21 at 03:14 -0700, vbossica@scdi.org wrote:
> Quintin,
>
> Thanks for your answer. My answer/querstions are inlined.
>
> > Besides the parameters added to your url "lang=" and "country=" isn't a
> > standard. That is just options used by google. There isn't even a guarentee
> > that any other sites in the world support them.
>
> the example with google is not representative and the idea would of
> course to be as generic as possible. For example, the addParameter
> method not called add<Key>Parameter(String value) but
> addParameter(String key, String value).
>
> one could imagine having methods for other types (addBooleanParameter,
> addIntParameter) or add methods similar to the one found in the
> commons-lang library (setDefautltParameter(String key, String value)
> would only add the parameter if the value is not blank, for example).
>
> Since this class is more of a UrlBuilder nature, java.net.URL doesn't
> fit very well (IIRC there's an enhancement request in the Sun bug
> database).
>
> > Encoding is usually passed through headers,
> > and therefore is related to the request object.
>
> I was thinking of the replacement of spaces in the query string.
>
> > If you want to do it like you asked though, create a URL delegate class.
> > Basically just a class that manages the URL and you expose methods like the
> > ones you mentioned.
>
> Of course this is one option. I was just hoping that -since it's a
> common problem- I would be able to find a common solution.
>
Vladimir,
We have a bunch of utility methods to facilitate the process of building
and parsing of the URL queries
http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/URLEncodedUtils.java
Usually this class suffices for our needs
> I can always try to write such a class using HttpCore components (e.g.
> HttpParams) and contribute it back to the project.
>
That would be a very welcome contribution. Please note
though HttpParams interface is intended to control the behavioral
aspects of HTTP components. It is not meant to represent URL query
parameters.
Cheers
Oleg
> Regards,
>
> -Vladimir
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org
Re: utility class to create URL strings
Posted by Quintin Beukes <qu...@last.za.net>.
Hey,
I understand what you meant.
You mean URL encoding, like " " = %20.
A URLBuilder would be a good way to describe what you want. Sorry, but I
don't know any like this.
I can't be difficult to make it yourself though. Supplying a basic and
splitting it with a tokenizer, and then building it on toString(). To encode
the URL you can use java.net.URLEncoder.encode(String s) to encode the
parts.
Q
On 6/21/08, vbossica@scdi.org <vb...@scdi.org> wrote:
>
> Quintin,
>
> Thanks for your answer. My answer/querstions are inlined.
>
> Besides the parameters added to your url "lang=" and "country=" isn't a
>> standard. That is just options used by google. There isn't even a
>> guarentee
>> that any other sites in the world support them.
>>
>
> the example with google is not representative and the idea would of course
> to be as generic as possible. For example, the addParameter method not
> called add<Key>Parameter(String value) but addParameter(String key, String
> value).
>
> one could imagine having methods for other types (addBooleanParameter,
> addIntParameter) or add methods similar to the one found in the commons-lang
> library (setDefautltParameter(String key, String value) would only add the
> parameter if the value is not blank, for example).
>
> Since this class is more of a UrlBuilder nature, java.net.URL doesn't fit
> very well (IIRC there's an enhancement request in the Sun bug database).
>
> Encoding is usually passed through headers,
>> and therefore is related to the request object.
>>
>
> I was thinking of the replacement of spaces in the query string.
>
> If you want to do it like you asked though, create a URL delegate class.
>> Basically just a class that manages the URL and you expose methods like
>> the
>> ones you mentioned.
>>
>
> Of course this is one option. I was just hoping that -since it's a common
> problem- I would be able to find a common solution.
>
> I can always try to write such a class using HttpCore components (e.g.
> HttpParams) and contribute it back to the project.
>
> Regards,
>
> -Vladimir
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>
--
Quintin Beukes
Re: utility class to create URL strings
Posted by vb...@scdi.org.
Quintin,
Thanks for your answer. My answer/querstions are inlined.
> Besides the parameters added to your url "lang=" and "country=" isn't a
> standard. That is just options used by google. There isn't even a guarentee
> that any other sites in the world support them.
the example with google is not representative and the idea would of
course to be as generic as possible. For example, the addParameter
method not called add<Key>Parameter(String value) but
addParameter(String key, String value).
one could imagine having methods for other types (addBooleanParameter,
addIntParameter) or add methods similar to the one found in the
commons-lang library (setDefautltParameter(String key, String value)
would only add the parameter if the value is not blank, for example).
Since this class is more of a UrlBuilder nature, java.net.URL doesn't
fit very well (IIRC there's an enhancement request in the Sun bug
database).
> Encoding is usually passed through headers,
> and therefore is related to the request object.
I was thinking of the replacement of spaces in the query string.
> If you want to do it like you asked though, create a URL delegate class.
> Basically just a class that manages the URL and you expose methods like the
> ones you mentioned.
Of course this is one option. I was just hoping that -since it's a
common problem- I would be able to find a common solution.
I can always try to write such a class using HttpCore components (e.g.
HttpParams) and contribute it back to the project.
Regards,
-Vladimir
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org
Re: utility class to create URL strings
Posted by Quintin Beukes <qu...@last.za.net>.
Besides the parameters added to your url "lang=" and "country=" isn't a
standard. That is just options used by google. There isn't even a guarentee
that any other sites in the world support them.
Encoding is usually passed through headers,
and therefore is related to the request object.
If you want to do it like you asked though, create a URL delegate class.
Basically just a class that manages the URL and you expose methods like the
ones you mentioned.
There won't be one exactly like that anywhere, since those parameters
are google specific.
You might find one that is used by the request to automatically fill in the
headers, but that is unlikely as well, because they are unrelated, and
therefore a proper design will put them into different objects.
Q
On 6/21/08, vbossica@scdi.org <vb...@scdi.org> wrote:
>
> All,
>
> I feel kind of stupid to ask such a simple question but I couldn't find the
> right library to do this simple job (and even asking around didn't return
> much valuable information).
>
> I would like to provide a handy way to (only) create a string URL
> definition, manage the parameters and handle the encoding. Something like:
>
> URLDefinition def = new URLDefinition("http://google.com");
> def.addParameter("lang", "de");
> def.addParameter("country", "ch");
> String a = def.toString();
>
> and a is magically "http://google.com?lang=de&country=ch" :-)
>
> I haven't seen anything ready-to-use in the various commons libraries
> (HttpParams is the closest I could find). Is there anything out there that
> I've been missing or should I simply roll my own version?
>
> Best regards,
>
> -Vladimir
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>
--
Quintin Beukes