Showing posts with label URL. Show all posts
Showing posts with label URL. Show all posts

Friday, 8 April 2011

Sending a soap message from soap client in java

Sending a soap message requires a server name, like --
http://localhost:13000/myservicename
13000 is port no.
Now just create a connection and write soap message to it.
We are using following imports:
import java.net.*;
import java.io.*;
Now just write the code:
URL u = new URL(server);
URLConnection uc = u.openConnection();
HttpURLConnection connection = (HttpURLConnection) uc;

connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("SOAPAction", SOAP_ACTION);

OutputStream out = connection.getOutputStream();
Writer wout = new OutputStreamWriter(out);

wout.write("<?xml version='1.0'?>\r\n");
wout.write("<SOAP-ENV:Envelope ");
wout.write("xmlns:SOAP-ENV=");
wout.write(
"'http://schemas.xmlsoap.org/soap/envelope/' "
);
wout.write("xmlns:xsi=");
wout.write(
"'http://www.w3.org/2001/XMLSchema-instance'>\r\n");
wout.write(" <SOAP-ENV:Body>\r\n");
wout.write(" <calculateFibonacci ");
wout.write(
"xmlns='http://namespaces.cafeconleche.org/xmljava/ch3/'\r\n"
);
wout.write(" type='xsi:positiveInteger'>" + input
+ "</calculateFibonacci>\r\n");
wout.write(" </SOAP-ENV:Body>\r\n");
wout.write("</SOAP-ENV:Envelope>\r\n");

wout.flush();
wout.close();

InputStream in = connection.getInputStream();
int c;
while ((c = in.read()) != -1) System.out.write(c);
in.close();

Sunday, 13 March 2011

How to validate URL in Java?

You can use org.apache.commons.validator.routines.UrlValidator class (ver 1.4) or apache.commons.validator.UrlValidator class to validate a given URL. The new UrlValidator is in the routines package and the apache.commons.validator.UrlValidator class will be removed in a future release. Here is an example,
public class ValidateUrlExample{

public static void main(String[] args) {
System.out.println("*** doUserSpecifiedSchemes ***");
doUserSpecifiedSchemes();
System.out.println("\n*** doDefaultSchemes ***");
doDefaultSchemes();
}

public static void doUserSpecifiedSchemes() {

String[] schemes = {"http","https"};
UrlValidator urlValidator = new UrlValidator(schemes);

String url = "http://foo.bar.com/";
if (urlValidator.isValid(url)) {
System.out.println(url + " is valid");
}
else {
System.out.println(url + " is invalid");
}

url = "ftp://foo.bar.com/";
if (urlValidator.isValid(url)) {
System.out.println(url + " is valid");
}
else {
System.out.println(url + " is invalid");
}

}

public static void doDefaultSchemes() {

UrlValidator urlValidator = new UrlValidator();

String url = "http://foo.bar.com/";
if (urlValidator.isValid(url)) {
System.out.println(url + " is valid");
}
else {
System.out.println(url + " is invalid");
}

url = "ftp://foo.bar.com/";
if (urlValidator.isValid(url)) {
System.out.println(url + " is valid");
}
else {
System.out.println(url + " is invalid");
}
}

}

output:
*** doUserSpecifiedSchemes ***
http://foo.bar.com/ is valid
ftp://foo.bar.com/ is invalid

*** doDefaultSchemes ***
http://foo.bar.com/ is valid
ftp://foo.bar.com/ is valid