WebServices Testing

What is WebService?

Web Services is the mechanism or the medium of communication through which two applications / machines will exchange the data irrespective of their underline architecture and the technology.

Why is WebService Needed?

In general, software applications are developed to be consumed by the human beings, where a person sends a request to a software service which in-turn returns a response in human readable format.

In the modern era of technology if you want to build a software application you don't need to build each and everything from scratch. There are lots of readymade services available which you can plug into your application and you can start providing those services in your application.

For example you want to display weather forecast information you don't need to collect, process and render the data in your application. You can buy the services from the people who already well-established in processing and publishing such kind of data.

Web services allow us to do these kind of implementations.

This WebService can be called by a Software Application using SOAP or HTTP protocol.

Web Services can be implemented in different ways, but the following two are the popular implementations approaches.

  1. SOAP (Simple Object Access Protocol)
  2. REST (Representational State Transfer architecture)

SOAP

SOAP is a standard protocol defined by the W3C Standard for sending and receiving web service requests and responses.

SOAP uses theXML format to send and receive the requestand hence the data is platform independent data. SOAP messages are exchanged between the provider applications and receiving application within the SOAP envelops.

As SOAP uses the simple http transport protocol, its messages are not got blocked by the firewalls.

Download soapui from the site :

WSDL – Web services description language

REST

REST means Representational State Transfer; it is an architecture that generally runs over HTTP. The REST style emphasizes the interactions between clients and services, which are enhanced by having a limited number of operations. REST is an alternative to SOAP (Simple Object Access Protocol) and instead of using XML for request REST uses simple URL in some cases. Unlike SOAP, RESTFUL applications uses HTTP build in headers to carry meta-information.

There are various code that REST use to determine whether user has access to API or not like code 200 or 201 indicates successful interaction with response body while 400 indicates a bad request or the request URI does not match the APIs in the system. All API request parameters and method parameters can be sent via eitherPOSTorGETvariables.

Rest API supports both XML and JSON format. It is usually preferred forMobileand web apps as it makes app work faster and smoother

publicclass NetClientGet {

publicstaticvoid main(String[] args) throws Exception {

URL url = new URL("

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setRequestMethod("GET");

conn.setRequestProperty("Accept", "application/json");

/*if (conn.getResponseCode() != 200) {

throw new RuntimeException("Failed : HTTP error code : "

+ conn.getResponseCode());

}*/

BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

String output;

System.out.println("Output from Server .... \n");

while ((output = br.readLine()) != null) {

System.out.println(output);

}

conn.disconnect();

}

}

//Post request

publicclass Rest_PostRequest {

publicstaticvoid main(String[] args) throws Exception {

try {

URL url = new URL("

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setDoOutput(true);

conn.setRequestMethod("POST");

conn.setRequestProperty("Content-Type", "application/json");

String input = "{\"qty\":100,\"name\":\"iPad 4\"}";

OutputStream os = conn.getOutputStream();

os.write(input.getBytes());

os.flush();

if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {

thrownewRuntimeException("Failed : HTTP error code : "

+ conn.getResponseCode());

}

BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

String output;

System.out.println("Output from Server .... \n");

while ((output = br.readLine()) != null) {

System.out.println(output);

}

conn.disconnect();

} catch (MalformedURLException e) {

e.printStackTrace();

}

}

}

API vs Web Service

API and Web service serve as a means of communication. The only difference is that a Web service facilitates interaction between two machines over a network. An API acts as an interface between two different applications so that they can communicate with each other. An API is a method by which the third-party vendors can write programs that interface easily with other programs. A Webservice is designedto have an interface that is depicted in a machine-processable format usually specified in Web Service Description Language (WSDL). Typically, “HTTP” is the most commonly usedprotocolfor communication. Web service also uses SOAP,REST, and XML-RPC as a means of communication. API may use any means of communication to initiate interaction between applications. For example, thesystemcalls are invoked using interrupts by theLinuxkernel API.

An API exactly defines the methods for onesoftwareprogram to interact with the other. When this action involves sendingdataover a network, Web services come into the picture. An API generally involves calling functions from within a software program.

In case of Web applications, the API used is web based. Desktop applications such as spreadsheets and word documents use VBA and COM-based APIs which don’t involve Web service. Aserverapplication such as Joomla may use a PHP-based API present within the server which doesn’t require Web service.

Read more:Difference Between API and Web Service | Difference Between