Dịch vụ Web và tập các giao
thức - Practice
Nguyễn Mạnh Tuấn
tuannm@[Link]
Nội dung
Practical SOAP Example
Using Web Service SOAP
Restful Web Service
2
Practical SOAP Example
This topic will look at using the [Link]
framework to build an ASMX web service. This type
of web service supports both SOAP version 1.1 and
version 1.2.
ASMX web services automatically generate the Web
Service Definition Language (WSDL) document.
This WSDL document is required by the calling
client application so that the application knows what
the web service is capable of doing.
Create [Link] Web application
From Visual Studio, click on the menu option File-
>New project
Create [Link] Web application
Project name: [Link]
Create [Link] Web application
Add a Web service file to project
Create [Link] Web application
Add the following code to your Tutorial Service
asmx file
Create [Link] Web application
F5 to run project
Create [Link] Web application
SOAP request and response
Create [Link] Web application
To show WSDL
Using Web Service SOAP
Add more web service
Create new project
Create new project
Create new project
Sử dụng WebService
Sử dụng WebService
Sử dụng WebService
static void Main(string[ ] args)
var client = new Guru99Webservice.Guru99WebserviceSoapClient();
[Link]([Link](l));
[Link]();
}
Output
Restful Web Service
What is Restful Web Service?
REST is used to build Web services that are
lightweight, maintainable, and scalable in nature. A
service which is built on the REST architecture is
called a RESTful service. The underlying protocol
for REST is HTTP, which is the basic web protocol.
REST stands for REpresentational State Transfer
RESTful Key Elements
Resources
to access an employee record resource via REST, one can issue
the command [Link]
Request Verbs
GET, POST, PUT, and DELETE...
Request Headers: additional instructions sent with the
request
Request Body: Data is sent with the request
Response Body: the main body of the response
Response Status codes: 200, 403...
Why Restful
Heterogeneous languages and
environments
The event of Devices
The event of the Cloud
Azure and Amazon provide a lot of API's
based on the Restful architecture
Hence, applications now need to be
developed in such a way that they are
made compatible with the Cloud
SOAP vs. REST
SOAP vs. REST
When to use REST and when to use SOAP
REST services should be used in the following
instances
Limited resources and bandwidth
Statelessness
Caching
SOAP
Asynchronous processing and subsequent invocation
A Formal means of communication
Stateful operations
Build RESTful web services
Create new Web project
As same SOAP project
Add service
Create new Web project
Add service
Create new Web project
Config: change enableWebScript to webHttp
Create new Web project
Add code:
[SenviceContnact(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode =
[Link]
public class TutorialService
{
private static List 1st = new List
(new String[] {"Arrays","Queues","Stacks"});
Create new Web project
Add code:
[WebGet(UriTemplate="/Tutorial")]
public String GetAllTutorial()
{
int count = [Link];
String TutorialList = "";
for (int i = 0; i < count; i++)
TutorialList = TutorialList + lst[i] + ",";
return TutorialList;
}
Create new Web project
Add code:
[WebGet (UriTemplate = "/Tutorial/{Tutorialid}")]
public String GetTutorialbyID(String Tutorialid)
{
int pid;
[Link](Tutorialid, out pid);
return lst[pid];
}
Create new Web project
Create new Web project
[WebInvoke(Method = "DELETE", RequestFormat = [Link],
UriTemplate = "/Tutorial/{Tutorialid}", ResponseFormat = [Link],
BodyStyle = [Link])]
public void DeleteTutorial(String Tutorialid)
{
int pid;
[Link](Tutorialid, out pid);
[Link](pid);
}
Test Web service
Test Web service
Test Web service
Thực thi POST, PUT, DELETE... Request
Run the Filddler tool
Test Web service
Build secure web services
Build project
The latest step of build SOAP. Add below code to exist web
service:
public class AuthHeader : SoapHeader
{
public string UserName;
public string Password;
}
Build project
public class TutorialService : [Link]
{
public AuthHeader Credentials;
[SoapHeader("Credentials")]
[WebMethod]
public string Guru99WebService()
{
if ([Link]() != "Guru99" ||
[Link]() != "Guru99Password")
{
throw new SoapException("Unauthorized",
[Link]);
}
eise
return "This is a Guru99 Web service";
}
Output
Client
static void Main(string[] args)
{
var client = new WebServiceSoapClient();
AuthHeader authHeader = new AuthHeader();
[Link] = "Guru99";
[Link] = "Guru99Password";
[Link]([Link](authHeader));
[Link]();
}
3/28/201
45
9