validation - Validate and encode urls containing unicode characters in Java -
i working on application in need validate urls , check if started http ( if not, prepend 'http') , encode them. problem urls receive can contain types of things - invalid / valid not starting http / encoded / valid containing spaces or unicode characters. using urlvalidator class, not validate spaces or unicode chars. following code:
if (url != null && !url.trim().isempty()) { url = urldecoder.decode(url, "utf-8"); if (!url.matches("^(https?)://.*$")) { url = "http" + url; } urlvalidator validator = new urlvalidator(); if (url.contains("(")) { if (validator.isvalid(url.substring(0, url.indexof("(")))) { return getencodedsiteurl(url); } return null; } if (validator.isvalid(url)) { return getencodedsiteurl(url); } } but code filters out valid urls contain space / unicode chars. don't think should use urlvalidator looking @ types of urls get. can please / guide me? thank you.
check this url has method may use.
public static boolean isurl(string url) { if (url == null) { return false; } // assigning url format regular expression string urlpattern = "^http(s{0,1})://[a-za-z0-9_/\\-\\.]+\\.([a-za-z/]{2,5})[a-za-z0-9_/\\&\\?\\=\\-\\.\\~\\%]*"; return url.matches(urlpattern); }
Comments
Post a Comment