4

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.

RB.
  • 36,301
  • 12
  • 91
  • 131
Sriks
  • 658
  • 4
  • 9
  • 18
  • 1
    I have tried this method as well as mentioned it in - http://stackoverflow.com/questions/11956750/how-to-test-the-availability-of-wsdl-programmatically-using-java – Sriks Feb 12 '13 at 15:57
  • +1 to that answer but I would recommend using `GET` instead of `HEAD`. – ach Feb 12 '13 at 16:45

3 Answers3

3

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

Yair Zaslavsky
  • 4,091
  • 4
  • 20
  • 27
  • This is NOT working... I have GOOD ONE --http://www.example:8080/helloService?wsdl BAD ONE -- http://www.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 – Sriks Feb 12 '13 at 16:27
  • @Sriks - this was not understood from your question. Now I Understand. You can open a URL connection. – Yair Zaslavsky Feb 12 '13 at 16:48
  • @Sriks - I modified my answer. – Yair Zaslavsky Feb 12 '13 at 16:50
  • Thanks for your suggestion. I just tried with valid and invalid url with different ports. And its still returning as GOOD URL. – Sriks Feb 12 '13 at 17:10
  • your suggestion gave me an idea to check with getContent() method call and try to solve the URL issue. Pls check below answer – Sriks Feb 12 '13 at 17:27
1

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?");
    } 
}

}
misterManSam
  • 24,303
  • 11
  • 69
  • 89
Sriks
  • 658
  • 4
  • 9
  • 18
0
import org.apache.commons.validator.UrlValidator;

public class 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/

Issam Ressani
  • 203
  • 1
  • 4
  • 7