开发者

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:

  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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜