Tuesday, July 27, 2010

Client code accessing a webservice

Soap Based webservice (Jax-ws) can be accessed in two ways. one is getting the wsdl file of the service and generating all the necessary artifacts using tool like wsgen. other way is accessing a webservice using online wsdl file locaiton and creating a service from it. following code shows how to access a webservice without any need of wsdl file at the compilation time.

code from my prototype project. This takes xml file as input and transfers the xml to the webserice.

URL wsdlLocation = new URL("http://10.144.135.225:9080/CimrsWeb/ReportWebService?wsdl");
QName serviceQName =
new QName("http://webservices.cimrs.dss.gov/", "ReportWebService");
QName portQName = new QName("http://webservices.cimrs.dss.gov/", "ReportWebPort");
Service service = Service.create(wsdlLocation, serviceQName);

Dispatch dispatch =
service.createDispatch(portQName, Source.class,
Service.Mode.PAYLOAD);
InputStream requestStream= new FileInputStream("C://input.xml");
Source request1 = new StreamSource(requestStream);

Source response1 = dispatch.invoke(request1);

Transformer copier = TransformerFactory.newInstance().newTransformer();

copier.transform(response1, new StreamResult(System.out));

No comments:

Post a Comment