java - 有没有简单的方法使用 JAVA 对 url 查询字符串进行语法验证

标签 java

有没有简单的方法可以使用 JAVA 对 url 查询字符串进行语法验证。我发现了类似的问题,但没有一个很好的答案。

最佳答案

您可以使用java.net.URL来创建URL。如果 URL 不正确,则会抛出 MalformedURLException

try{  
    URL url=new URL("www.something.com?a=b&c=d");  
}
catch(MalformedURLException e){
    System.out.println("Sorry! This is Incorrect URL!!!");
}  

请参阅documentation

URL

public URL(String spec) throws MalformedURLException

Creates a URL object from the String representation. This constructor is equivalent to a call to the two-argument constructor with a null first argument.

Parameters:
spec - the String to parse as a URL.

Throws:
MalformedURLException - if no protocol is specified, or an unknown protocol is found, or spec is null.

关于java - 有没有简单的方法使用 JAVA 对 url 查询字符串进行语法验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42800984/

相关文章:

javascript - Java从浏览器读取不同body长度的字节流

java - 为什么 Java 不允许通过实例方法隐藏静态方法?

java - 无法在我的应用程序中使用 SWT 星云小部件

java - 我怎样才能让firebase短链接脱离任务

java - 从被调用方法内的函数返回

Java - 当声明全局变量并用值初始化时,对象创建时会发生什么?

java - 绑定(bind)不匹配 : The type MyClass1 is not a valid substitute for the bounded parameter <T extends Comparator<T>> of the type Person<T>

java - 如何在服务器上托管jsp网站(页面)?

java - 如何使用正弦/余弦波返回振荡数

java - 尝试从 url 下载图像时,出现缓冲区容量错误