Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standard Java Class for common URL/URI manipulation

Tags:

java

This question has almost certainly been asked before, but I ask it anyway because I couldn't find an answer.

Generally, is there a utility class of some sort that assists in common String manipulations associated with URL/URIs?

I'm thinking something like Java SE's URL Class, but maybe a little beefier. I'm looking for something that will let you do simple things, like:

  • Get a List of query string parameters
  • An "addParameter" method to add a query string parameter, and it will take care of adding "&", "?", and "=" where necessary
  • Also, encoding parameter values would be ideal...

Let me know, thanks!

like image 913
JasonStoltz Avatar asked May 06 '11 15:05

JasonStoltz


People also ask

Which classes are used for manipulating urls?

net. URL class represents a URL and has a complete set of methods to manipulate URL in Java. Sr.No. Creates a URL by putting together the given parts.

Does Java have a URI class?

In general, an URI (Uniform Resource Identifier) represents a resource. The URI class of Java represents this format You can get the URI format of a file by invoking the toURI() method.

What is the URL class in Java?

Class URL represents a Uniform Resource Locator, a pointer to a "resource" on the World Wide Web. A resource can be something as simple as a file or a directory, or it can be a reference to a more complicated object, such as a query to a database or to a search engine.

What is URI and URL in Java?

A URI is a uniform resource identifier while a URL is a uniform resource locator. Hence every URL is a URI, abstractly speaking, but not every URI is a URL. This is because there is another subcategory of URIs, uniform resource names (URNs), which name resources but do not specify how to locate them.


1 Answers

There isn't really (oddly enough) any standard that does it all. There are some bits and pieces, usually buried in various util packages:

I've used http://java.net/projects/urlencodedquerystring/pages/Home to decent effect (for extraction of parameters).

Atlassian's JIRA has http://docs.atlassian.com/jira/4.2/index.html?com/atlassian/jira/util/UrlBuilder.html, which I've actually extracted from the jar and used.

On Android, http://developer.android.com/reference/android/net/Uri.Builder.html is a Uri builder that works pretty well as far as building a url with ease.

And finally, in a classic case of history repeating itself: A good library to do URL Query String manipulation in Java.

I'd really just rip out the android.net.Uri.Builder class and pair that with the urlencodedquerystring class and then carry those around with you, but this does seem like a good candidate for an Apache commons package.

like image 158
Femi Avatar answered Sep 18 '22 22:09

Femi