有没有简单的方法可以使用 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!!!");
}
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/