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

20,495

Solution 1

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

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

Solution 2

Try this.. Its working for me now. Thanks @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?");
    } 
}

}
Share:
20,495
Sriks
Author by

Sriks

Updated on November 30, 2020

Comments

  • Sriks
    Sriks over 3 years

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

    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

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

  • Sriks
    Sriks about 11 years
    This is NOT working... I have GOOD ONE --example:8080/helloService?wsdl BAD ONE -- example:8081/helloService?wsdl . But from above code both URLs are returns as Good Url. I wanted to check the Service is up or not instead of just URL syntax
  • Yair Zaslavsky
    Yair Zaslavsky about 11 years
    @Sriks - this was not understood from your question. Now I Understand. You can open a URL connection.
  • Yair Zaslavsky
    Yair Zaslavsky about 11 years
    @Sriks - I modified my answer.
  • Sriks
    Sriks about 11 years
    Thanks for your suggestion. I just tried with valid and invalid url with different ports. And its still returning as GOOD URL.
  • Sriks
    Sriks about 11 years
    your suggestion gave me an idea to check with getContent() method call and try to solve the URL issue. Pls check below answer
  • user1428716
    user1428716 about 11 years
    Why to check getContent() ? .. cant we do with getResponseCode() ?