U6 M1 L9 Annotated - Tagged

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Developing Applications Using Web

Services

Janaka Balasooriya, Ph.D.


Lecturer, Barrett Honors Faculty
Arizona State University
Objectives

Applications that Use Web Client for Math


Web Services Web Service

Discuss how application Analyze how to develop a Web


developers use web services Client using C# and .NET
including the three-party model constructs to consume both SOAP
in SOC. and REST Math Web Services.
Applications Using Web Services

Client applications
that use web services
can vary.

(Examples include
mobile apps, desktop
applications, web
applications, etc.)
Applications Using Web Services

Clients can use SOAP,


REST or both types of
web services at the
same time.
Applications Using Web Services

Language and
platform independent
loosely coupled
nature of web service
applications are
clearly visible in this
scenario as client
technology doesn’t
depend on the web
service technology
The Three-Party Model of Service-Oriented Software
Development

Developers who
understand database,
End User ontology, and matching White
pages

Yellow
Software Registry pages
engineers who Service Brokers Traditional
understand the Green
pages
object-oriented
application programmers,
domain Service active objects
Repository hosting
Internet

Applications Active Objects

Application Builders Service Providers

Service Requesters Service Developers


Math Web Client Example
| We will illustrate the
development of a web client
application for our SOAP
and REST math web
services.
| The client application will
use the .NET framework in
Visual Studio and
developed using C#
programming language.
| Similar client applications
can be developed using
other similar technologies
and programming
languages such as Java,
Python, JavaScript, etc.
Math Web Client Example - Steps
Create a website application using visual studio and design the UI.

Steps
1. New➞Project➞Web➞
ASP .Net website
application
2. Select empty project.
3. Add a web form to the
empty project.
4. Open the toolbox and add
labels, textboxes and
buttons as shown.
Math Web Client Example: Calling SOAP Web Service

| Add the web service


WSDL URL as the
service reference.
| Use the service
reference to create a
proxy object.

soapMathRef.MathServiceClient soapProxy = new


soapMathRef.MathServiceClient();
protected void Button1_Click(object sender, EventArgs e)
{
string str = soapText.Text;
soapMathRef.results res =
soapProxy.computeStat(str);
soapRes.Text = res.digits.ToString();
[Serializable]
} public class results
{
public int digits { get; set; }
public int upper_case_letters { get;
set; }
public int lower_case_letters { get;
set; }
Math Web Client Example: Calling SOAP Web Service

| Call the web service


method computeStat
using the proxy when
the button is clicked.

soapMathRef.MathServiceClient soapProxy = new


soapMathRef.MathServiceClient();
protected void Button1_Click(object sender, EventArgs e)
{
string str = soapText.Text;
soapMathRef.results res =
soapProxy.computeStat(str);
soapRes.Text = res.digits.ToString();
} [Serializable]
public class results
{
public int digits { get; set; }
public int upper_case_letters { get; set; }
public int lower_case_letters { get; set; }
}
Math Web Client Example: Calling SOAP Web Service

| Since the computeStat


method is returning
results object, define
the structure of the
results object in the
client.

soapMathRef.MathServiceClient soapProxy = new


soapMathRef.MathServiceClient();
protected void Button1_Click(object sender, EventArgs e)
{
string str = soapText.Text;
soapMathRef.results res =
soapProxy.computeStat(str);
soapRes.Text = res.digits.ToString();
[Serializable]
} public class results
{
public int digits { get; set; }
public int upper_case_letters { get;
set; }
public int lower_case_letters { get;
set; }
Math Web Client Example - Calling SOAP Web Service

Complete Client Code


public partial class Default : System.Web.UI.Page
{
soapMathRef.MathServiceClient soapProxy = new
soapMathRef.MathServiceClient();

protected void Button1_Click(object sender,


EventArgs e)
{
string str = soapText.Text;
soapMathRef.results res =
soapProxy.computeStat(str);
soapRes.Text = res.digits.ToString();

[Serializable]
public class results
{
public int digits { get; set; }
public int upper_case_letters { get; set; }
public int lower_case_letters { get; set; }
}
}
Math Web Client Example: Calling REST Web Service
Math Web Client Example: Calling REST Web Service

protected void Button2_Click(object sender,


EventArgs e)
{

string str = restText.Text;


string url =
@"http://webstrar99.fulton.asu.edu/page1/MathService
.svc/computeStatJson?str=" + str;

HttpWebRequest request =
(HttpWebRequest)WebRequest.Create(url);
WebResponse response =
request.GetResponse();
Stream responseStream =
response.GetResponseStream();

StreamReader reader = new


StreamReader(responseStream);
String json = reader.ReadToEnd();
restUnprocessed.Text = json;

results r =
JsonConvert.DeserializeObject<results>(json);
Math Web Client Example: Calling REST Web Service

protected void Button2_Click(object sender,


EventArgs e)
Call the web {
service method
using string str = restText.Text;
computeStat string url =
using HTTP client @"http://webstrar99.fulton.asu.edu/page1/MathService
object by passing .svc/computeStatJson?str=" + str;
the data through
URL HttpWebRequest request =
(HttpWebRequest)WebRequest.Create(url);
WebResponse response =
request.GetResponse();
Stream responseStream =
response.GetResponseStream();

StreamReader reader = new


StreamReader(responseStream);
String json = reader.ReadToEnd();
restUnprocessed.Text = json;

results r =
JsonConvert.DeserializeObject<results>(json);
Math Web Client Example: Calling REST Web Service

protected void Button2_Click(object sender,


EventArgs e)
Since the {
computeStat
method is string str = restText.Text;
returning results string url =
object, define the @"http://webstrar99.fulton.asu.edu/page1/MathService.
structure of the svc/computeStatJson?str=" + str;
results object in
the client. HttpWebRequest request =
(HttpWebRequest)WebRequest.Create(url);
WebResponse response =
request.GetResponse();
Stream responseStream =
response.GetResponseStream();
[Serializable]
StreamReader
public class results reader = new
StreamReader(responseStream);
{
public int digits
String json {= get; set; }
reader.ReadToEnd();
public int upper_case_letters={ json;
restUnprocessed.Text get;
set; }
public int lower_case_letters { get;
set; }
results r =
JsonConvert.DeserializeObject<results>(json);
}
Summary
| Web Services, either REST or SOAP, provide loosely
coupled service to client relationships.
| Client technology and platforms can be different from
web service development platforms.
| Client programming languages can be different from web
service development languages.
| SOAP uses a proxy object based on the WSDL web
service interface to facilitate client-to-server
communication.
| REST uses HTTP-based URL to facilitate clients to
access REST APIs.
| Complex user-defined types will be returned as objects
in SOAP, while REST uses HTTP to invoke Web Service
APIs that may return XML or JSON.

You might also like