在 Java 中验证 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1600291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Validating URL in Java
提问by Keya
I wanted to know if there is any standard APIs in Java to validate a given URL? I want to check both if the URL string is right i.e. the given protocol is valid and then to check if a connection can be established.
我想知道 Java 中是否有任何标准 API 来验证给定的 URL?我想检查 URL 字符串是否正确,即给定的协议是否有效,然后检查是否可以建立连接。
I tried using HttpURLConnection, providing the URL and connecting to it. The first part of my requirement seems to be fulfilled but when I try to perform HttpURLConnection.connect(), 'java.net.ConnectException: Connection refused' exception is thrown.
我尝试使用 HttpURLConnection,提供 URL 并连接到它。我的要求的第一部分似乎得到了满足,但是当我尝试执行 HttpURLConnection.connect() 时,会抛出“java.net.ConnectException: Connection denied”异常。
Can this be because of proxy settings? I tried setting the System properties for proxy but no success.
这可能是因为代理设置吗?我尝试为代理设置系统属性,但没有成功。
Let me know what I am doing wrong.
让我知道我做错了什么。
回答by NickDK
Are you sure you're using the correct proxy as system properties?
您确定使用正确的代理作为系统属性吗?
Also if you are using 1.5 or 1.6 you could pass a java.net.Proxy instance to the openConnection() method. This is more elegant imo:
此外,如果您使用 1.5 或 1.6,您可以将 java.net.Proxy 实例传递给 openConnection() 方法。这是更优雅的imo:
//Proxy instance, proxy ip = 10.0.0.1 with port 8080
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.0.0.1", 8080));
conn = new URL(urlString).openConnection(proxy);
回答by Olly
You need to create both a URL
object and a URLConnection
object. The following code will test both the format of the URL and whether a connection can be established:
您需要创建一个URL
对象和一个URLConnection
对象。以下代码将测试 URL 的格式以及是否可以建立连接:
try {
URL url = new URL("http://www.yoursite.com/");
URLConnection conn = url.openConnection();
conn.connect();
} catch (MalformedURLException e) {
// the URL is not in a valid form
} catch (IOException e) {
// the connection couldn't be established
}
回答by Keya
Thanks. Opening the URL connection by passing the Proxy as suggested by NickDK works fine.
谢谢。按照 NickDK 的建议,通过传递代理来打开 URL 连接工作正常。
//Proxy instance, proxy ip = 10.0.0.1 with port 8080
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.0.0.1", 8080));
conn = new URL(urlString).openConnection(proxy);
System properties however doesn't work as I had mentioned earlier.
然而,系统属性并不像我之前提到的那样工作。
Thanks again.
再次感谢。
Regards, Keya
问候, 凯亚
回答by Doc Davluz
Just important to point that the URL object handle both validation and connection. Then, only protocols for which a handler has been provided in sun.net.www.protocolare authorized (file, ftp, gopher, http, https, jar, mailto, netdoc) are valid ones. For instance, try to make a new URL with the ldapprotocol:
重要的是要指出 URL 对象同时处理验证和连接。然后,只有在sun.net.www.protocol中提供了处理程序的协议才被授权(file、 ftp、gopher、http、https、jar、mailto、netdoc)是有效的。例如,尝试使用ldap协议创建一个新 URL :
new URL("ldap://myhost:389")
You will get a java.net.MalformedURLException: unknown protocol: ldap
.
你会得到一个java.net.MalformedURLException: unknown protocol: ldap
.
You need to implement your own handler and register it through URL.setURLStreamHandlerFactory()
. Quite overkill if you just want to validate the URL syntax, a regexp seems to be a simpler solution.
您需要实现自己的处理程序并通过URL.setURLStreamHandlerFactory()
. 如果您只想验证 URL 语法,那就有点矫枉过正了,正则表达式似乎是一个更简单的解决方案。
回答by Yonatan
For the benefit of the community, since this thread is top on Google when searching for
"url validator java"
为了社区的利益,因为在搜索
“ url validator java”时该线程在 Google 上名列前茅
Catching exceptions is expensive, and should be avoided when possible. If you just want to verify your String is a valid URL, you can use the UrlValidatorclass from the Apache Commons Validatorproject.
捕获异常代价高昂,应尽可能避免。如果您只想验证您的字符串是否是有效的 URL,您可以使用Apache Commons Validator项目中的UrlValidator类。
For example:
例如:
String[] schemes = {"http","https"}; // DEFAULT schemes = "http", "https", "ftp"
UrlValidator urlValidator = new UrlValidator(schemes);
if (urlValidator.isValid("ftp://foo.bar.com/")) {
System.out.println("URL is valid");
} else {
System.out.println("URL is invalid");
}
回答by Martin
The java.net.URL
class is in fact not at all a good way of validating URLs. MalformedURLException
is notthrown on all malformed URLs during construction. Catching IOException
on java.net.URL#openConnection().connect()
does not validate URL either, only tell wether or not the connection can be established.
该java.net.URL
班其实不是在所有验证URL的一个好办法。MalformedURLException
是不是扔在施工期间的所有格式不正确的URL。追赶IOException
上java.net.URL#openConnection().connect()
不验证URL要么,只能告诉羯羊或不是可以建立连接。
Consider this piece of code:
考虑这段代码:
try {
new URL("http://.com");
new URL("http://com.");
new URL("http:// ");
new URL("ftp://::::@example.com");
} catch (MalformedURLException malformedURLException) {
malformedURLException.printStackTrace();
}
..which does not throw any exceptions.
..它不会抛出任何异常。
I recommend using some validation API implemented using a context free grammar, or in very simplified validation just use regular expressions. However I need someone to suggest a superior or standard API for this, I only recently started searching for it myself.
我建议使用一些使用上下文无关语法实现的验证 API,或者在非常简化的验证中只使用正则表达式。但是我需要有人为此建议一个高级或标准的 API,我最近才开始自己搜索它。
NoteIt has been suggested that URL#toURI()
in combination with handling of the exception java.net. URISyntaxException
can facilitate validation of URLs. However, this method only catches one of the very simple cases above.
注意有人建议URL#toURI()
结合异常处理java.net. URISyntaxException
可以促进 URL 的验证。但是,此方法仅捕获上述非常简单的情况之一。
The conclusion is that there is no standard java URL parser to validate URLs.
结论是没有标准的 java URL 解析器来验证 URL。
回答by b1nary.atr0phy
Using onlystandard API, pass the string to a URL
object then convert it to a URI
object. This will accurately determine the validity of the URL according to the RFC2396 standard.
仅使用标准 API,将字符串传递给URL
对象,然后将其转换为URI
对象。这将根据 RFC2396 标准准确确定 URL 的有效性。
Example:
例子:
public boolean isValidURL(String url) {
try {
new URL(url).toURI();
} catch (MalformedURLException | URISyntaxException e) {
return false;
}
return true;
}
回答by penduDev
Use the android.webkit.URLUtil
on android:
android.webkit.URLUtil
在安卓上使用:
URLUtil.isValidUrl(URL_STRING);
Note: It is just checking the initial scheme of URL, not that the entire URL is valid.
注意:它只是检查 URL 的初始方案,而不是整个 URL 是否有效。
回答by dened
There is a way to perform URL validation in strict accordance to standards in Java without resorting to third-party libraries:
有一种方法可以严格按照 Java 中的标准执行 URL 验证,而无需求助于第三方库:
boolean isValidURL(String url) {
try {
new URI(url).parseServerAuthority();
return true;
} catch (URISyntaxException e) {
return false;
}
}
The constructor of URI
checks that url
is a valid URI, and the call to parseServerAuthority
ensures that it is a URL (absolute or relative) and not a URN.
URI
检查的构造函数url
是一个有效的 URI,并调用以parseServerAuthority
确保它是一个 URL(绝对或相对)而不是一个 URN。
回答by Genaut
I think the best response is from the user @b1nary.atr0phy. Somehow, I recommend combine the method from the b1nay.atr0phy response with a regex to cover all the possible cases.
我认为最好的回应来自用户@b1nary.atr0phy。不知何故,我建议将 b1nay.atr0phy 响应中的方法与正则表达式结合起来,以涵盖所有可能的情况。
public static final URL validateURL(String url, Logger logger) {
URL u = null;
try {
Pattern regex = Pattern.compile("(?i)^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[/?#]\S*)?$");
Matcher matcher = regex.matcher(url);
if(!matcher.find()) {
throw new URISyntaxException(url, "La url no está formada correctamente.");
}
u = new URL(url);
u.toURI();
} catch (MalformedURLException e) {
logger.error("La url no está formada correctamente.");
} catch (URISyntaxException e) {
logger.error("La url no está formada correctamente.");
}
return u;
}