WEB SERVICES LAB PROGRAMS M.Tech

WEB SERVICES LAB PROGRAMS


1.     WRITE A PROGRAM TO IMPLEMENT WSDL SERVICE ( HELLO SERVICE. WSDL FILE)
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions
   targetNamespace="http://t320.open.ac.uk"
xmlns:apachesoap="http://xml.apache.org/xml-soap"
xmlns:impl="http://t320.open.ac.uk"
xmlns:intf="http://t320.open.ac.uk"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!--WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)-->
<wsdl:types>
<schema
elementFormDefault="qualified"
targetNamespace="http://t320.open.ac.uk"
xmlns="http://www.w3.org/2001/XMLSchema">
<element name="helloName">
<complexType>
<sequence>
<element name="name" type="xsd:string" />
</sequence>
</complexType>
</element>
<element name="helloNameResponse">
<complexType>
<sequence>
<element name="helloNameReturn" type="xsd:string" />
</sequence>
</complexType>
</element>
</schema>
</wsdl:types>
<wsdl:message name="helloNameResponse">
<wsdl:part element="impl:helloNameResponse" name="parameters" />
</wsdl:message>
<wsdl:message name="helloNameRequest">
<wsdl:part element="impl:helloName" name="parameters" />
</wsdl:message>
<wsdl:portType name="Hello">
<wsdl:operation name="helloName">
<wsdl:input message="impl:helloNameRequest" name="helloNameRequest" />
<wsdl:output message="impl:helloNameResponse" name="helloNameResponse" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloSoapBinding" type="impl:Hello">
<wsdlsoap:binding
style="document" transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="helloName">
<wsdlsoap:operation soapAction="" />
<wsdl:input name="helloNameRequest">
<wsdlsoap:body use="literal" />
</wsdl:input>
<wsdl:output name="helloNameResponse">
<wsdlsoap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloService">
<wsdl:port binding="impl:HelloSoapBinding" name="Hello">
<wsdlsoap:address
location="http://localhost:8180/Hello/services/Hello" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
2.     WRITE A PROGRAM THE SERVICE PROVIDER CAN BE IMPLEMENT A SINGLE  GET PRICE(), STATIC BIND() AND GET PRODUCT OPERATION.

getprice()
package com.ecerami.soap.examples;
import java.util.Hashtable;
/**
* A Sample SOAP Service
* Provides Current Price for requested Stockkeeping Unit (SKU)
*/
public class PriceService {
protected Hashtable products;
/**
* Zero Argument Constructor
* Load product database with two sample products
*/
public PriceService ( ) {
products = new Hashtable( );
// Red Hat Linux
products.put("A358185", new Double (54.99));
// McAfee PGP Personal Privacy
products.put("A358565", new Double (19.99));
}
/**
* Provides Current Price for requested SKU
* In a real-setup, this method would connect to
* a price database. If SKU is not found, method
* will throw a PriceException.
*/
public double getPrice (String sku)
throws ProductNotFoundException {
Double price = (Double) products.get(sku);
if (price == null) {
throw new ProductNotFoundException ("SKU: "+sku+" not found");
}
return price.doubleValue( );
}
}
To generate a WSDL file for this class, run the following command:
java2wsdl com.ecerami.soap.examples.PriceService -s -e http://localhost:
8080/soap/servlet/rpcrouter -n urn:examples:priceservice
The -s option directs GLUE to create a SOAP binding; the -e option specifies the address of
our service; and the -n option specifies the namespace URN for the service. GLUE will generate
a PriceService.wsdl file.
Getproduct()
<?xml version="1.0" encoding="UTF-8"?>
<definitions name="ProductService"
targetNamespace="http://www.ecerami.com/wsdl/ProductService.wsdl"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://www.ecerami.com/wsdl/ProductService.wsdl"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsd1="http://www.ecerami.com/schema">
<types>
<xsd:schema
targetNamespace="http://www.ecerami.com/schema"
xmlns="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="product">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="description" type="xsd:string"/>
<xsd:element name="price" type="xsd:double"/>
<xsd:element name="SKU" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</types>
<message name="getProductRequest">
<part name="sku" type="xsd:string"/>
</message>
<message name="getProductResponse">
<part name="product" type="xsd1:product"/>
</message>
<portType name="Product_PortType">
<operation name="getProduct">
<input message="tns:getProductRequest"/>
<output message="tns:getProductResponse"/>
</operation>
</portType>
<binding name="Product_Binding" type="tns:Product_PortType">
<soap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="getProduct">
<soap:operation soapAction="urn:examples:productservice"/>
<input>
<soap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="urn:examples:productservice"
use="encoded"/>
</input>
<output>
<soap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="urn:examples:productservice" use="encoded"/>
</output>
</operation>
</binding>
<service name="Product_Service">
<port name="Product_Port" binding="tns:Product_Binding">
<soap:address
location="http://localhost:8080/soap/servlet/rpcrouter"/>
</port>
</service>
</definitions>
3.     WRITE APROGRAM TO IMPLEMENT THE OPERATION CAN RECEIVE REQUEST AND WILL RETURN A RESPONSE IN TWO WAYS.
A)  ONE-WAY OPERATION
A one-way operation example:
<message name="newTermValues">
  <part name="term" type="xs:string"/>
  <part name="value" type="xs:string"/>
</message>

<portType name="glossaryTerms">
  <operation name="setTerm">
    <input name="newTerm" message="newTermValues"/>
  </operation>
</portType >

B)   REQUEST- RESPONSE
A request-response operation example:
<message name="getTermRequest">
  <part name="term" type="xs:string"/>
</message>

<message name="getTermResponse">
  <part name="value" type="xs:string"/>
</message>

<portType name="glossaryTerms">
  <operation name="getTerm">
    <input message="getTermRequest"/>
    <output message="getTermResponse"/>
  </operation>
</portType>

A request-response operation example:
<message name="getTermRequest">
  <part name="term" type="xs:string"/>
</message>

<message name="getTermResponse">
  <part name="value" type="xs:string"/>
</message>

<portType name="glossaryTerms">
  <operation name="getTerm">
    <input message="getTermRequest"/>
    <output message="getTermResponse"/>
  </operation>
</portType>

<binding type="glossaryTerms" name="b1">
   <soap:binding style="document"
   transport="http://schemas.xmlsoap.org/soap/http" />
   <operation>
     <soap:operation soapAction="http://example.com/getTerm"/>
     <input><soap:body use="literal"/></input>
     <output><soap:body use="literal"/></output>
  </operation>
</binding>

4.     WRITE APROGRAM TO IMPLEMENT TO CREATE A SIMPLE WEB SERVICE THAT CONVERTS THE TEMPERATURE FROM FAHRENHEIT TO CELSIUS ( USING HTTP POST PROTOCOL)

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
public string FahrenheitToCelsius(string Fahrenheit)
{
object fahr = null;
fahr = Fahrenheit.Replace(",", ".").Trim(' ');
if (fahr == "")
{
return "Error";
}
int returnVal = ((((Convert.ToInt32(fahr)) - 32) / 9) * 5);
return returnVal.ToString();
}
[WebMethod]
public string CelsiusToFahrenheit(string Celsius)
{
object cel = null;
cel = Celsius.Replace(",", ".").Trim(' ');
if (cel == "")
{
return "Error";
}
int returnVal = ((((Convert.ToInt32(cel)) * 9) / 5) + 32);
return returnVal.ToString();
}
}
5.     WRITE APROGRAM TO IMPLEMENT BUSINESS UDDI REGISTRY ENTRY



6.     WRITE APROGRAM TO IMPLEMENT 
A)  WEB BASED SERVICE CONSUMER 

Web-Based Service Consumer

Write a Web-based consumer as given below. Call it WebApp.aspx. Note that it is an ASP.NET application. Save this in the virtual directory of the Web Service (c:\MyWebServices\WebApp.axpx).
This application has two text fields that are used to get numbers from the user to be added. It has one button, Execute, that, when clicked, gets the Add and SayHello Web Services.
 
WebApp.axpx
  <%@ Page Language="C#" %>
  <script runat="server">
  void runSrvice_Click(Object sender, EventArgs e)
  {
      FirstService mySvc = new FirstService();
      Label1.Text = mySvc.SayHello();
      Label2.Text = mySvc.Add(Int32.Parse(txtNum1.Text),
                    Int32.Parse(txtNum2.Text)).ToString();
  }
  </script>
  <html>
  <head>
  </head>
  <body>
  <form runat="server">
    <p>
        <em>First Number to Add </em>:
        <asp:TextBox id="txtNum1" runat="server"
             Width="43px">4</asp:TextBox>
    </p>
    <p>
        <em>Second Number To Add </em>:
        <asp:TextBox id="txtNum2" runat="server"
             Width="44px">5</asp:TextBox>
    </p>
    <p>
        <strong><u>Web Service Result -</u></strong>
    </p>
    <p>
        <em>Hello world Service</em> :
        <asp:Label id="Label1" runat="server"
             Font-Underline="True">Label</asp:Label>
    </p>
 
    <p>
        <em>Add Service</em> :
        & <asp:Label id="Label2" runat="server"
               Font-Underline="True">Label</asp:Label>
    </p>
    <p align="left">
        <asp:Button id="runSrvice" onclick="runSrvice_Click"
             runat="server" Text="Execute"></asp:Button>
    </p>
  </form>
  </body>
  </html>
After the consumer is created, we need to create a proxy for the Web Service to be consumed. This work is done automatically by Visual Studio .NET for us when referencing a Web Service that has been added. Here are the steps to be followed:
  • Create a proxy for the Web Service to be consumed. The proxy is created using the wsdl utility supplied with the .NET SDK. This utility extracts information from the Web Service and creates a proxy. Thus, the proxy created is valid only for a particular Web Service. If you need to consume other Web Services, you need to create a proxy for this service as well. VS .NET creates a proxy automatically for you when the reference for the Web Service is added. Create a proxy for the Web Service using the wsdl utility supplied with the .NET SDK. It will create FirstSevice.cs in the current directory. We need to compile it to create FirstService.dll (proxy) for the Web Service.
c:> WSDL http://localhost/MyWebServices/
 
                FirstService.asmx?WSDL
c:> csc /t:library FirstService.cs
  • Put the compiled proxy in the bin directory of the virtual directory of the Web Service (c:\MyWebServices\bin). IIS looks for the proxy in this directory.
  • Create the service consumer, which we have already done. Note that I have instantiated an object of the Web Service proxy in the consumer. This proxy takes care of interacting with the service.
  • Type the URL of the consumer in IE to test it (for example, http://localhost/MyWebServices/WebApp.aspx).

B)WINDOWS APPLICATION BASED WEB SERVICE CONSUMER.

Windows Application-Based Web Service Consumer

Writing a Windows application-based Web Service consumer is the same as writing any other Windows application. The only work to be done is to create the proxy (which we have already done) and reference this proxy when compiling the application. Following is our Windows application that uses the Web Service. This application creates a Web Service object (of course, proxy) and calls the SayHello and Add methods on it.
WinApp.cs
  using System;
  using System.IO;
 
  namespace SvcConsumer{
  class SvcEater
  {
      public static void Main(String[] args)
      {
          FirstService mySvc = new FirstService();
 
          Console.WriteLine("Calling Hello World Service: " +
                             mySvc.SayHello());
          Console.WriteLine("Calling Add(2, 3) Service: " +
                             mySvc.Add(2, 3).ToString());
      }
  }
  }
Compile it using c:>csc /r:FirstService.dll WinApp.cs. It will create WinApp.exe. Run it to test the application and the Web Service.


No comments: