如何从 Java 验证 WSDL URL 是否已启动并正在运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14836407/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:33:46  来源:igfitidea点击:

How to validate WSDL URL is up and running or not from Java?

javaweb-servicesurlwsdl

提问by Sriks

I wanted to check WSDL url is valid or not from Java code. So that I can use another url based on that.

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

So I am checking the code like this.

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

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

But its alwys returning false though I have valid URL. WSDL URL Ex:http://www.sample.com/MyWsdlService?wsdl

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

Because URL ends with ?wsdl How to check the code? Looks we need only "http://www.sample.com" for UrlValidator to pass.

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

回答by Yair Zaslavsky

You're trying to validate a WSDL URL? Why not use the java.net.URL class ?
And do something like:

您正在尝试验证 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?");
}

When I inserted bad URLs like htt2p instead of http I got - "bad url" print

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

回答by Sriks

Try this.. Its working for me now. Thanks @zaske

试试这个..它现在对我有用。谢谢@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?");
    } 
}

}

回答by Issam Ressani

import org.apache.commons.validator.UrlValidator;

public class ValidateUrlExample{

公共类 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");
    }}

}

for more informations: https://www.mkyong.com/java/how-to-validate-url-in-java/

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