You are on page 1of 10

WEB SERVICES LAB

Objectives:
To implement the technologies like WSDL, UDDI.
To learn how to implement and deploy web service client and server
1) Write a program to implement WSL Service !"ello Service#$s%l &ile)
<!ml version"#$.%# encoding"#UT&'(#)
<de*initions name"#+elloService#
target,amespace"#http-..www.ecerami.com.wsdl.+elloService.wsdl#
!mlns"#http-..schemas.!mlsoap.org.wsdl.#
!mlns-soap"#http-..schemas.!mlsoap.org.wsdl.soap.#
!mlns-tns"#http-..www.ecerami.com.wsdl.+elloService.wsdl#
!mlns-!sd"#http-..www.w/.org.0%%$.12LSchema#)

<message name"#Say+ello3e45est#)
<part name"#*irst,ame# type"#!sd-string#.)
<.message)
<message name"#Say+ello3esponse#)
<part name"#greeting# type"#!sd-string#.)
<.message)

<portType name"#+ello67ortType#)
<operation name"#say+ello#)
<inp5t message"#tns-Say+ello3e45est#.)
<o5tp5t message"#tns-Say+ello3esponse#.)
<.operation)
<.portType)

<binding name"#+ello68inding# type"#tns-+ello67ortType#)
<soap-binding style"#rpc#
transport"#http-..schemas.!mlsoap.org.soap.http#.)
<operation name"#say+ello#)
<soap-operation soap9ction"#say+ello#.)
<inp5t)
<soap-body
encodingStyle"#http-..schemas.!mlsoap.org.soap.encoding.#
namespace"#5rn-e!amples-helloservice#
5se"#encoded#.)
<.inp5t)
<o5tp5t)
<soap-body
encodingStyle"#http-..schemas.!mlsoap.org.soap.encoding.#
namespace"#5rn-e!amples-helloservice#
5se"#encoded#.)
<.o5tp5t)
<.operation)
<.binding)

<service name"#+ello6Service#)
<doc5mentation)WSDL &ile *or +elloService<.doc5mentation)
<port binding"#tns-+ello68inding# name"#+ello67ort#)
<soap-address
location"#http-..localhost-(%(%.soap.servlet.rpcro5ter#.)
<.port)
<.service)
<.de*initions)
') Write a program t(e service provi%er can be implement a single get)rice!)* static bin%!)
an% get)ro%+ct operation#
package com.ecerami.soap.e!amples:
import ;ava.5til.+ashtable:
.<<
< 9 Sample S=97 Service
< 7rovides >5rrent 7rice *or re45ested Stockkeeping Unit ?S@UA
<.
p5blic class 7riceService B
protected +ashtable prod5cts:

.<<
< Cero 9rg5ment >onstr5ctor
< Load prod5ct database with two sample prod5cts
<.
p5blic 7riceService ? A B
prod5cts " new +ashtable? A:
.. 3ed +at Lin5!
prod5cts.p5t?#9/D($(D#, new Do5ble ?DE.FFAA:
.. 2c9*ee 7G7 7ersonal 7rivacy
prod5cts.p5t?#9/D(DHD#, new Do5ble ?$F.FFAA:
I

.<<
< 7rovides >5rrent 7rice *or re45ested S@U
< In a real'set5p, this method wo5ld connect to
< a price database. I* S@U is not *o5nd, method
< will throw a 7riceJ!ception.
<.
p5blic do5ble get7rice ?String sk5A
throws 7rod5ct,ot&o5ndJ!ception B
Do5ble price " ?Do5bleA prod5cts.get?sk5A:
i* ?price "" n5llA B
throw new 7rod5ct,ot&o5ndJ!ception ?#S@U- #Ksk5K# not *o5nd#A:
I
ret5rn price.do5bleLal5e? A:
I
I
<!ml version"M$.%M encoding"MUT&'(M)
<N''generated by GLUJ'')
<de*initions name"Mcom.ecerami.soap.e!amples.7riceServiceM
target,amespace"Mhttp-..www.themindelectric.com.wsdl.com.ecerami.soap.
e!amples.7riceService.M

!mlns-tns"Mhttp-..www.themindelectric.com.wsdl.com.ecerami.soap.
e!amples.7riceService.M
!mlns-electric"Mhttp-..www.themindelectric.com.M
!mlns-soap"Mhttp-..schemas.!mlsoap.org.wsdl.soap.M
!mlns-http"Mhttp-..schemas.!mlsoap.org.wsdl.http.M
!mlns-mime"Mhttp-..schemas.!mlsoap.org.wsdl.mime.M
!mlns-!sd"Mhttp-..www.w/.org.0%%$.12LSchemaM
!mlns-soapenc"Mhttp-..schemas.!mlsoap.org.soap.encoding.M
!mlns-wsdl"Mhttp-..schemas.!mlsoap.org.wsdl.M
!mlns"Mhttp-..schemas.!mlsoap.org.wsdl.M)
<message name"Mget7rice%SoapInM)
<part name"Msk5M type"M!sd-stringM.)
<.message)
<message name"Mget7rice%Soap=5tM)
<part name"M3es5ltM type"M!sd-do5bleM.)
<.message)
<portType name"Mcom.ecerami.soap.e!amples.7riceServiceSoapM)
<operation name"Mget7riceM parameter=rder"Msk5M)
<inp5t name"Mget7rice%SoapInM message"Mtns-get7rice%SoapInM.)
<o5tp5t name"Mget7rice%Soap=5tM message"Mtns-get7rice%Soap=5tM.)
<.operation)
<.portType)
<binding name"Mcom.ecerami.soap.e!amples.7riceServiceSoapM
type"Mtns-com.ecerami.soap.e!amples.7riceServiceSoapM)
<soap-binding style"MrpcM
transport"Mhttp-..schemas.!mlsoap.org.soap.httpM.)
<operation name"Mget7riceM)
<soap-operation soap9ction"Mget7riceM style"MrpcM.)
<inp5t name"Mget7rice%SoapInM)
<soap-body 5se"MencodedM
namespace"M5rn-e!amples-priceserviceM
encodingStyle"Mhttp-..schemas.!mlsoap.org.soap.encoding.M.)
<.inp5t)
<o5tp5t name"Mget7rice%Soap=5tM)
<soap-body 5se"MencodedM
namespace"M5rn-e!amples-priceserviceM
encodingStyle"Mhttp-..schemas.!mlsoap.org.soap.encoding.M.)
<.o5tp5t)
<.operation)
<.binding)
<service name"Mcom.ecerami.soap.e!amples.7riceServiceM)
<port name"Mcom.ecerami.soap.e!amples.7riceServiceSoapM
binding"Mtns-com.ecerami.soap.e!amples.7riceServiceSoapM)
<soap-address location"Mhttp-..0%O.0/O.0%$.$(O-(%(%
.soap.servlet. rpcro5terM.)
<.port)
<.service)
<.de*initions)
b) get pro%+ct operation
<!ml version"#$.%#)
<de*initions name"#e8ayWatcherService#
target,amespace"
#http-..www.!methods.net.sd.e8ayWatcherService.wsdl#
!mlns-tns"#http-..www.!methods.net.sd.e8ayWatcherService.wsdl#
!mlns-!sd"#http-..www.w/.org.0%%$.12LSchema#
!mlns-soap"#http-..schemas.!mlsoap.org.wsdl.soap.#
!mlns"#http-..schemas.!mlsoap.org.wsdl.#)

<message name"#get>5rrent7rice3e45est#)
<part name"#a5ction6id# type " #!sd-string#.)
<.message)
<message name"#get>5rrent7rice3esponse#)
<part name"#ret5rn# type " #!sd-*loat#.)
<.message)

<portType name"#e8ayWatcher7ortType#)
<operation name"#get>5rrent7rice#)
<inp5t
message"#tns-get>5rrent7rice3e45est#
name"#get>5rrent7rice#.)
<o5tp5t
message"#tns-get>5rrent7rice3esponse#
name"#get>5rrent7rice3esponse#.)
<.operation)
<.portType)

<binding name"#e8ayWatcher8inding# type"#tns-e8ayWatcher7ortType#)
<soap-binding
style"#rpc#
transport"#http-..schemas.!mlsoap.org.soap.http#.)
<operation name"#get>5rrent7rice#)
<soap-operation soap9ction"##.)
<inp5t name"#get>5rrent7rice#)
<soap-body
5se"#encoded#
namespace"#5rn-!methods'JbayWatcher#
encodingStyle"#http-..schemas.!mlsoap.org.soap.encoding.#.)
<.inp5t)
<o5tp5t name"#get>5rrent7rice3esponse#)
<soap-body
5se"#encoded#
namespace"#5rn-!methods'JbayWatcher#
encodingStyle"#http-..schemas.!mlsoap.org.soap.encoding.#.)
<.o5tp5t)
<.operation)
<.binding)

<service name"#e8ayWatcherService#)
<doc5mentation)
>hecks c5rrent high bid *or an e8ay a5ction
<.doc5mentation)
<port name"#e8ayWatcher7ort# binding"#tns-e8ayWatcher8inding#)
<soap-address
location"#http-..services.!methods.net-(%.soap.servlet.rpcro5ter#.)
<.port)
<.service)
<.de*initions)
,#a) Write a program to implement t(e operation can receive re-+est an% $ill ret+rn
response in One.Wa/ operation#
import ;ava!.!ml.soap.<:
import ;ava!.!ml.namespace.P,ame:
import ;ava.5til.Iterator:
import ;ava.net.U3L:
p5blic class 3e45est B
p5blic static void main?StringQR argsA B
try B
S=97>onnection&actory soap>onnection&actory "
S=97>onnection&actory.newInstance?A:
S=97>onnection connection "
soap>onnection&actory.create>onnection?A:
2essage&actory *actory " 2essage&actory.newInstance?A:
S=972essage message " *actory.create2essage?A:
S=97+eader header " message.getS=97+eader?A:
S=978ody body " message.getS=978ody?A:
header.detach,ode?A:
P,ame body,ame " new P,ame?#http-..wombat.Strade.com#,
#GetLastTrade7rice#, #m#A:
S=978odyJlement bodyJlement " body.add8odyJlement?body,ameA:
P,ame name " new P,ame?#symbol#A:
S=97Jlement symbol " bodyJlement.add>hildJlement?nameA:
symbol.addTe!t,ode?#SU,W#A:
U3L endpoint " new U3L?#http-..wombat.Strade.com.45otes#A:
S=972essage response " connection.call?message, endpointA:
connection.close?A:
S=978ody soap8ody " response.getS=978ody?A:
Iterator iterator " soap8ody.get>hildJlements?body,ameA:
bodyJlement " ?S=978odyJlementAiterator.ne!t?A:
String last7rice " bodyJlement.getLal5e?A:
System.o5t.print?#The last price *or SU,W is #A:
System.o5t.println?last7riceA:
I catch ?J!ception e!A B
e!.printStackTrace?A:
I
I
I
4.Write a program to implement to create a simple web service that converts the temperature from
Fahrenheit to Celsius (using HTTP Post Protocol)
The following is a sample SOAP 1.1 request and response. The placeholders shown need to be replaced with
actual values.
POST /webservices/tempconvert.asmx HTTP/1.1
Host: www.w3schools.com
Content-Type: text/xml charset!"t#-$
Content-%en&th: len&th
SO'P'ction: (http://www.w3schools.com/webservices/Celsi"sTo)ahrenheit(
*+xml version!(1.,( enco-in&!("t#-$(+.
*soap:/nvelope xmlns:xsi!(http://www.w3.or&/0,,1/12%Schema-instance(
xmlns:xs-!(http://www.w3.or&/0,,1/12%Schema(
xmlns:soap!(http://schemas.xmlsoap.or&/soap/envelope/(.
*soap:3o-y.
*Celsi"sTo)ahrenheit xmlns!(http://www.w3schools.com/webservices/(.
*Celsi"s.strin&*/Celsi"s.
*/Celsi"sTo)ahrenheit.
*/soap:3o-y.
*/soap:/nvelope.
HTTP/1.1 0,, O4
Content-Type: text/xml charset!"t#-$
Content-%en&th: len&th
*+xml version!(1.,( enco-in&!("t#-$(+.
*soap:/nvelope xmlns:xsi!(http://www.w3.or&/0,,1/12%Schema-instance(
xmlns:xs-!(http://www.w3.or&/0,,1/12%Schema(
xmlns:soap!(http://schemas.xmlsoap.or&/soap/envelope/(.
*soap:3o-y.
*Celsi"sTo)ahrenheit5esponse
xmlns!(http://www.w3schools.com/webservices/(.
*Celsi"sTo)ahrenheit5es"lt.strin&*/Celsi"sTo)ahrenheit5es"lt.
*/Celsi"sTo)ahrenheit5esponse.
*/soap:3o-y.
*/soap:/nvelope.
D. S=97 envelope *or the registration
<S=97'J,L- Jnvelope
!mlns-S=97'J,L"#http-..schemas.!mlsoap.org.soap.envelope.#
!mlns-!si"#http-..www.w/.org.$FFF.12LSchema'instance#
!mlns-!sd"#http-..www.w/.org.$FFF.12LSchema#)
<S=97'J,L-8ody)
<save6b5siness generic"#$.%# !mlns"#5rn-5ddi'org-api#)
<a5thln*o)test<.a5thln*o)
<b5sinessJntity)
<name)3ichard +5angMs<.name)
<b5sinessServices)
<b5sinessService)
<name)+ello World Service<.name)
<bindingTemplates)
<bindingTemplate)
<access7oint H$
5rlType"#+TT7#)http-..localhost-(%(%<.access7oint)
<.bindingTemplate)
<.bindingTemplates)
<.b5sinessService)
<.b5sinessServices)
<identi*ier8ag)
<keyed3e*erence key,ame"#D'U','S#
keyLalLie"#l0/EDHO(F%#
T2odel@ey"#UUID-(H%F>(lJ'JJl&'EDD9'80%0'/J8l/9D=l(0/#.)
<.identi*ier8ag)
<category8ag)
<keyed3e'*erence key,ame"#,9I>S#
keyLal5e"T$$$FE#
T+odel@ey"#UUID->=8F&J$/'$OF&'E$/D'(9D8'D%%ED8(JD880#.)
<.category8ag)
<.b5sinessJntity)
<.save6b5siness)
<.S=97'J,L-8ody)
<.S=97'J,L- Jnvelope
H.Write a program to implement
aA Web based service cons5mer
b) FirstService.asmx
c6 *78 9ebService lan&"a&e!(C( class!()irstService( 7.
-6
e6 "sin& System
#6 "sin& System.9eb.Services
&6 "sin& System.1ml.Seriali:ation
h6
i6 ;9ebService<=amespace!(http://localhost/2y9ebServices/(6>
?6 p"blic class )irstService : 9ebService
@6 A
l6 ;9eb2etho->
m6 p"blic int '--<int aB int b6
n6 A
o6 ret"rn a C b
p6 D
E6
r6 ;9eb2etho->
s6 p"blic Strin& SayHello<6
t6 A
"6 ret"rn (Hello 9orl-(
v6 D
w6 D

b) Windows Application-Based Web Service ConsuWEB
winApp.cs
"sin& System
"sin& System.FO
namespace SvcCons"merA
class Svc/ater
A
p"blic static voi- 2ain<Strin&;> ar&s6
A
)irstService mySvc ! new )irstService<6
Console.9rite%ine<(Callin& Hello 9orl- Service: ( C
mySvc.SayHello<66
Console.9rite%ine<(Callin& '--<0B 36 Service: ( C
mySvc.'--<0B 36.ToStrin&<66
D
D
D