Reputation: 678
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.
Upvotes: 4
Views: 21463
Reputation: 213
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/
Upvotes: 0
Reputation: 678
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?");
}
}
}
Upvotes: 1
Reputation: 4137
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
Upvotes: 3