Best Practice: How to ensure and modify a String so that it is a valid URL
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:
What is the best way to detect if a URL is a well-formed URL?
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
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.
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.
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.
精彩评论