3

我想从 Java 代码中检查 WSDL url 是否有效。这样我就可以使用另一个基于此的 url。

所以我正在检查这样的代码。

private boolean isValidWsdlURL(String wsdlUrl) 
{
    UrlValidator urlValidator = new UrlValidator();
    if(urlValidator.isValid(wsdlUrl)) 
    {
        return true;
    }
    logger.info("WSDL URL is not valid...");
    return false;
}

但是,尽管我有有效的 URL,但它总是返回 false。 WSDL URL 例如: http ://www.sample.com/MyWsdlService?wsdl

因为 URL 以 ?wsdl 结尾如何查看代码?看起来我们只需要“ http://www.sample.com ”就可以通过 UrlValidator。

4

3 回答 3

3

您正在尝试验证 WSDL URL?为什么不使用 java.net.URL 类?
并执行以下操作:

String urlStr = "http://www.example.com/helloService?wsdl";
URL url = null;
try {
  url = new URL(urlStr);
  URLConnection urlConnection = url.openConnection()
} catch (MalformedURLException ex) {
   System.out.println("bad URL");
} catch (IOException ex) {
   System.out.println("Failed opening connection. Perhaps WS is not up?");
}

当我插入像 htt2p 而不是 http 这样的错误 URL 时,我得到了 - “bad url” print

于 2013-02-12T16:04:58.490 回答
1

试试这个..它现在对我有用。谢谢@zaske

public class TestWSDL {

public static void main(String args[]) {
    String urlStr = "http://www.example.com:8080/helloService?wsdl";
    URL url = null;
    URLConnection urlConnection = null;
    try {
      url = new URL(urlStr);
      urlConnection = url.openConnection();
      if(urlConnection.getContent() != null) {
          System.out.println("GOOD URL");
      } else {
          System.out.println("BAD URL");
      }
    } catch (MalformedURLException ex) {
       System.out.println("bad URL");
    } catch (IOException ex) {
       System.out.println("Failed opening connection. Perhaps WS is not up?");
    } 
}

}
于 2013-02-12T17:28:26.557 回答
0
import org.apache.commons.validator.UrlValidator;

公共类 ValidateUrlExample{

public static void main(String[] args) {

    UrlValidator urlValidator = new UrlValidator();

    //valid URL
    if (urlValidator.isValid("http://www.mkyong.com")) {
       System.out.println("url is valid");
    } else {
       System.out.println("url is invalid");
    }

    //invalid URL
    if (urlValidator.isValid("http://invalidURL^$&%$&^")) {
        System.out.println("url is valid");
    } else {
        System.out.println("url is invalid");
    }}

}

欲了解更多信息: https ://www.mkyong.com/java/how-to-validate-url-in-java/

于 2016-11-29T10:24:47.763 回答