tags:

views:

48

answers:

3

I have a application which requires the user to input some values. From these values a URL is generated. However if the user enters special symbols like e.g. german vowels or other strange symbol, there is an exception when I try to create a URL. So i have two questions:

  1. What is the best way to detect if a URL is a well-formed URL?

  2. How should i convert a invalid URL into a valid URL? I know this want work, but I'd like if from a invalid URL a valid URL is generated that is as similar as possible to the original URL: for example http://www.äüö.com is modified to http://www.auo.com

+2  A: 

I'm not sure you should do that anymore, since now one can register domain names using native characters, like www.sãopaulo.com , which could be different from www.saopaulo.com . That's also valid for russian, chinese, etc.

Eduardo
yes there are some special characters which are allowed, however e.g. whitespaces aren't possible.
Roflcoptr
yeah, I think you should read the specifications for the new domain names characters to make sure what is or not allowed. I guess it's become even trickier now.
Eduardo
+1  A: 

I would strongly suggest that you do not attempt to correct (in my mind "guess") a user-entered URL. The security concerns alone are nightmarish. If an invalid URL is entered, throw an exception and proceed accordingly.

Bob Kaufman
ok, but how can i check without trying to execute if a URL is well formed?
Roflcoptr
+1  A: 

java.net.URL in the Android Java runtime behaves much like the Sun Java class of the same name. Its constructor throws a MalformedURLException if the URL is malformed.

Alan Krueger