SlideShare a Scribd company logo
Web Service – Chuyện nhỏ! Phạm Minh Tuấn  Khoa CNTT - ĐH KHTN
Nội dung trình bày Web Service là gì vậy? Cho vài ví dụ đi… Làm sao gọi thực hiện một Web Service có sẵn? Tạo một Web Service như thế nào? Tản mạn chuyện hậu trường
Web Service là cái gì vậy? GetStudentInfo(int Id) Web Service
Cho vài ví dụ đi… Xem   thông  tin  thời   tiết Dịch   tự   động Kiểm   tra  Card Number Xem   thông  tin  Sân  bay Tỉ   giá   ngoại   tệ   Kiểm   tra  Email … .
Làm sao gọi thực hiện  một Web Service có sẵn? Gọi từ Windows Application Kiểm   tra  Email Gọi từ Web Application: Tương tự Windows Application Tỉ   giá   ngoại   tệ Gọi từ Client Script Tỉ   giá   ngoại   tệ
Làm sao gọi thực hiện  một Web Service có sẵn? Gọi từ Windows và Web Application Add Web Reference Và sử dụng như các thư viện bình thường khác Ví dụ: private void button1_Click(object sender, EventArgs e) { net.webservicex.www.TranslateService t = new net.webservicex.www.TranslateService(); textBox2.Text= t.Translate(net.webservicex.www.Language.EnglishTOGerman, textBox1.Text); }
Làm sao gọi thực hiện  một Web Service có sẵn? Gọi từ Client Script(1) Gắn kết Webservice behavior vào trang web. Chú ý file  webservice.htc  phải nằm cùng thư mục với trang web. Onresult  cho phép xác định hàm sẽ được gọi tự động sau khi Server thực hiện xong Web service và trả kết quả về. <div id=&quot;service&quot; style=&quot;behavior:url(webservice.htc)&quot; onresult=&quot;ShowResult()&quot;> </div>
Làm sao gọi thực hiện  một Web Service có sẵn? Gọi từ Client Script(2) Xác định Web Service sẽ được sử dụng bằng hàm useService.  service.useService(&quot;http://trial.serviceobjects.com/ce/CurrencyExchange.asmx?wsdl&quot;,&quot;CurrencyExchange&quot;); Gọi thực hiện Web Service bằng hàm callService  service.CurrencyExchange.callService(&quot;ConvertCurrency&quot;,”10”,”USD”,”VND”,&quot;WS30-HKM3-JNZ4&quot;);
Làm sao gọi thực hiện  một Web Service có sẵn? Gọi từ Client Script(3) Xử lý kết quả trả về Lấy giá trị bằng  event.result.value Kiểm tra có lỗi hay không thông qua  event.result.error Nếu có lỗi thì xem thông tin lỗi qua  event.result.errorDetail.string   function ShowResult() {  if (event.result.error) document.DemoForm.BoxText.value = event.result.errorDetail.string;  else document.DemoForm.BoxText.value = event.result.value;  }
Tạo một Web Service  như thế nào? Xử lý với các kiểu dữ liệu cơ sở <WebMethod()> Public Function GetAge(ByVal Month As Integer, ByVal Day As Integer, ByVal Year As Integer) As Integer Dim Birthday As New DateTime(Year, Month, Day) If (Year < 1900) Then GetAge = -1 ElseIf (Year > Now.Year) Then GetAge = -1 ElseIf (Month < 1) Or (Month > 12) Then GetAge = -1 ElseIf (Day < 1) Or (Day > 31) Then GetAge = -1 Else GetAge = New DateTime(Now.Ticks - Birthday.Ticks).Year - 1 End If End Function
Tạo một Web Service  như thế nào? Xử lý với mảng <WebMethod()> Public Function Add(ByRef Values As Double(), ByVal Increment As Double) As Integer Dim Entry As Double Dim I As Integer For I = 0 To Values.Length - 1 Values(I) += Increment Next Return 0 End Function
Tạo một Web Service  như thế nào? Xử lý với đối tượng Public Structure ServerInfo Dim OS As String Dim DotNet As Boolean Dim SSL As Boolean Dim Administrator As String Dim WebServer As String End Structure <WebMethod()> Public Function GetInfo() As ServerInfo Dim Result As New ServerInfo Result.OS = &quot;Windows&quot; Result.DotNet = True Result.SSL = True Result.Administrator = &quot;B. Gates&quot; Result.WebServer = &quot;IIS&quot; GetInfo = Result End Function
Tạo một Web Service  như thế nào? Xử lý với CSDL <WebMethod()> Public Function GetEntries(ByVal TableName As String)  As String()  Dim Entries(1) As  String  Dim Connection  As  New SqlConnection(&quot;Initial Catalog=Db1; Data Source=(local);User ID=sa;password=;&quot;)  Connection.Open()  Dim Query As String = &quot;SELECT * From &quot; & TableName  Dim DataSetObj  As  New DataSet()  Dim Adapter As SqlDataAdapter = New _ SqlDataAdapter(Query, Connection) Dim CmdBuilder As SqlCommandBuilder = New SqlCommandBuilder(Adapter) Adapter.Fill(DataSetObj)   Dim I As Integer  For I = 0 To DataSetObj.Tables(0).Rows.Count - 1  ReDim Preserve Entries(I + 1)  Entries(I) = DataSetObj.Tables(0).Rows(I).ItemArray(1)  Next  Connection.Close()  GetEntries = Entries End Function
Tản mạn chuyện hậu trường…
Tản mạn chuyện hậu trường… WSDL: Web Service Description Language   Dùng để mô tả các thông tin về Web Service: Tên phương thức, các tham số đầu vào, kiểu trả về… Ví dụ:  File  WSDL   tương   ứng   với   GetAge <WebMethod()> Public Function GetAge(ByVal Month As Integer, ByVal Day As Integer, ByVal Year As Integer) As Integer …… . End Function
Tản mạn chuyện hậu trường… SOAP: Simple Object Access Protocol   protocol for exchanging XML-based messages over computer network Ví dụ thông điệp gửi đi từ Client <soap:Envelope xmlns:soap=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;> <soap:Body>  <getProductDetails xmlns=&quot;http://warehouse.example.com/ws&quot;> <productID>827635</productID>  </getProductDetails> </soap:Body> </soap:Envelope> Ví dụ thông điệp gửi về từ Server  <soap:Envelope xmlns:soap=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;> <soap:Body>  <getProductDetailsResponse xmlns=&quot;http://warehouse.example.com/ws&quot;>  <getProductDetailsResult>  <productName>Toptimate 3-Piece Set</productName> <productID>827635</productID>  <description>3-Piece luggage set. Black Polyester.</description> <price>96.50</price> <inStock>true</inStock>  </getProductDetailsResult> </getProductDetailsResponse> </soap:Body> </soap:Envelope>
Tản mạn chuyện hậu trường… UDDI: Universal Description Discovery Integration  a “yellow pages” for web services that exist worldwide
Q & A… Cám ơn các bạn đã theo dõi

More Related Content

PPT
Slide Web Service
DOCX
Web service
PPTX
Tìm hiểu web service
PPT
PDF
(HoaND) giao trinh webservice
PDF
Bài thuyết trình môn công nghệ web
PPT
Ung dun web chuong 2
DOCX
Triển Khai Mail Exchange 2007 .
Slide Web Service
Web service
Tìm hiểu web service
(HoaND) giao trinh webservice
Bài thuyết trình môn công nghệ web
Ung dun web chuong 2
Triển Khai Mail Exchange 2007 .

Viewers also liked (15)

PPT
Bài 7 - Web Services Asp.net
PPT
Business process excution language
PPT
Asynchronous PHP. Myth? Reality!
ODP
The promise of asynchronous PHP
PDF
What Makes Great Infographics
PDF
Masters of SlideShare
PDF
STOP! VIEW THIS! 10-Step Checklist When Uploading to Slideshare
PDF
You Suck At PowerPoint!
PDF
10 Ways to Win at SlideShare SEO & Presentation Optimization
PDF
How To Get More From SlideShare - Super-Simple Tips For Content Marketing
PDF
A Guide to SlideShare Analytics - Excerpts from Hubspot's Step by Step Guide ...
PDF
2015 Upload Campaigns Calendar - SlideShare
PPTX
What to Upload to SlideShare
PDF
How to Make Awesome SlideShares: Tips & Tricks
PDF
Getting Started With SlideShare
Bài 7 - Web Services Asp.net
Business process excution language
Asynchronous PHP. Myth? Reality!
The promise of asynchronous PHP
What Makes Great Infographics
Masters of SlideShare
STOP! VIEW THIS! 10-Step Checklist When Uploading to Slideshare
You Suck At PowerPoint!
10 Ways to Win at SlideShare SEO & Presentation Optimization
How To Get More From SlideShare - Super-Simple Tips For Content Marketing
A Guide to SlideShare Analytics - Excerpts from Hubspot's Step by Step Guide ...
2015 Upload Campaigns Calendar - SlideShare
What to Upload to SlideShare
How to Make Awesome SlideShares: Tips & Tricks
Getting Started With SlideShare
Ad

Similar to Web Services (20)

PPT
Asp
PPT
Ung dung web chuong 5
PPT
User Control
PDF
Giao trinh asp.ne_tvoi_csharp
PPTX
LINQ presentation
DOC
đề thi trắc nghiệm asp net co kem đáp án trên 400 câu hỏi thiết kế website ki...
DOC
400 câu hỏi thi trắc nghiệm ASP.NET có đáp án - Thiết kế website kinh doanh 2
PPT
Videoconferencereport
PDF
Asp control
PPT
Introduction Vs2008 Dot Net35
PDF
Net06 asp.net applications & state management
PDF
Bài 1: Làm quen với ASP.NET - Giáo trình FPT - Có ví dụ kèm theo
DOCX
Ajax Control ToolKit
DOC
Bao cao do an ltm hoan chinh
DOCX
Ajax report
PPT
C# co ban 9
PDF
Giáo trình asp.net với c sharp
PDF
Hdth.chuong5 ado.netv2.0
PPTX
Tài liệu hướng dẫn sử dụng javascript cho lập trình web
PPT
Ung dung web chuong 7
Asp
Ung dung web chuong 5
User Control
Giao trinh asp.ne_tvoi_csharp
LINQ presentation
đề thi trắc nghiệm asp net co kem đáp án trên 400 câu hỏi thiết kế website ki...
400 câu hỏi thi trắc nghiệm ASP.NET có đáp án - Thiết kế website kinh doanh 2
Videoconferencereport
Asp control
Introduction Vs2008 Dot Net35
Net06 asp.net applications & state management
Bài 1: Làm quen với ASP.NET - Giáo trình FPT - Có ví dụ kèm theo
Ajax Control ToolKit
Bao cao do an ltm hoan chinh
Ajax report
C# co ban 9
Giáo trình asp.net với c sharp
Hdth.chuong5 ado.netv2.0
Tài liệu hướng dẫn sử dụng javascript cho lập trình web
Ung dung web chuong 7
Ad

Web Services

  • 1. Web Service – Chuyện nhỏ! Phạm Minh Tuấn Khoa CNTT - ĐH KHTN
  • 2. Nội dung trình bày Web Service là gì vậy? Cho vài ví dụ đi… Làm sao gọi thực hiện một Web Service có sẵn? Tạo một Web Service như thế nào? Tản mạn chuyện hậu trường
  • 3. Web Service là cái gì vậy? GetStudentInfo(int Id) Web Service
  • 4. Cho vài ví dụ đi… Xem thông tin thời tiết Dịch tự động Kiểm tra Card Number Xem thông tin Sân bay Tỉ giá ngoại tệ Kiểm tra Email … .
  • 5. Làm sao gọi thực hiện một Web Service có sẵn? Gọi từ Windows Application Kiểm tra Email Gọi từ Web Application: Tương tự Windows Application Tỉ giá ngoại tệ Gọi từ Client Script Tỉ giá ngoại tệ
  • 6. Làm sao gọi thực hiện một Web Service có sẵn? Gọi từ Windows và Web Application Add Web Reference Và sử dụng như các thư viện bình thường khác Ví dụ: private void button1_Click(object sender, EventArgs e) { net.webservicex.www.TranslateService t = new net.webservicex.www.TranslateService(); textBox2.Text= t.Translate(net.webservicex.www.Language.EnglishTOGerman, textBox1.Text); }
  • 7. Làm sao gọi thực hiện một Web Service có sẵn? Gọi từ Client Script(1) Gắn kết Webservice behavior vào trang web. Chú ý file webservice.htc phải nằm cùng thư mục với trang web. Onresult cho phép xác định hàm sẽ được gọi tự động sau khi Server thực hiện xong Web service và trả kết quả về. <div id=&quot;service&quot; style=&quot;behavior:url(webservice.htc)&quot; onresult=&quot;ShowResult()&quot;> </div>
  • 8. Làm sao gọi thực hiện một Web Service có sẵn? Gọi từ Client Script(2) Xác định Web Service sẽ được sử dụng bằng hàm useService. service.useService(&quot;http://trial.serviceobjects.com/ce/CurrencyExchange.asmx?wsdl&quot;,&quot;CurrencyExchange&quot;); Gọi thực hiện Web Service bằng hàm callService service.CurrencyExchange.callService(&quot;ConvertCurrency&quot;,”10”,”USD”,”VND”,&quot;WS30-HKM3-JNZ4&quot;);
  • 9. Làm sao gọi thực hiện một Web Service có sẵn? Gọi từ Client Script(3) Xử lý kết quả trả về Lấy giá trị bằng event.result.value Kiểm tra có lỗi hay không thông qua event.result.error Nếu có lỗi thì xem thông tin lỗi qua event.result.errorDetail.string function ShowResult() { if (event.result.error) document.DemoForm.BoxText.value = event.result.errorDetail.string; else document.DemoForm.BoxText.value = event.result.value; }
  • 10. Tạo một Web Service như thế nào? Xử lý với các kiểu dữ liệu cơ sở <WebMethod()> Public Function GetAge(ByVal Month As Integer, ByVal Day As Integer, ByVal Year As Integer) As Integer Dim Birthday As New DateTime(Year, Month, Day) If (Year < 1900) Then GetAge = -1 ElseIf (Year > Now.Year) Then GetAge = -1 ElseIf (Month < 1) Or (Month > 12) Then GetAge = -1 ElseIf (Day < 1) Or (Day > 31) Then GetAge = -1 Else GetAge = New DateTime(Now.Ticks - Birthday.Ticks).Year - 1 End If End Function
  • 11. Tạo một Web Service như thế nào? Xử lý với mảng <WebMethod()> Public Function Add(ByRef Values As Double(), ByVal Increment As Double) As Integer Dim Entry As Double Dim I As Integer For I = 0 To Values.Length - 1 Values(I) += Increment Next Return 0 End Function
  • 12. Tạo một Web Service như thế nào? Xử lý với đối tượng Public Structure ServerInfo Dim OS As String Dim DotNet As Boolean Dim SSL As Boolean Dim Administrator As String Dim WebServer As String End Structure <WebMethod()> Public Function GetInfo() As ServerInfo Dim Result As New ServerInfo Result.OS = &quot;Windows&quot; Result.DotNet = True Result.SSL = True Result.Administrator = &quot;B. Gates&quot; Result.WebServer = &quot;IIS&quot; GetInfo = Result End Function
  • 13. Tạo một Web Service như thế nào? Xử lý với CSDL <WebMethod()> Public Function GetEntries(ByVal TableName As String) As String() Dim Entries(1) As String Dim Connection As New SqlConnection(&quot;Initial Catalog=Db1; Data Source=(local);User ID=sa;password=;&quot;) Connection.Open() Dim Query As String = &quot;SELECT * From &quot; & TableName Dim DataSetObj As New DataSet() Dim Adapter As SqlDataAdapter = New _ SqlDataAdapter(Query, Connection) Dim CmdBuilder As SqlCommandBuilder = New SqlCommandBuilder(Adapter) Adapter.Fill(DataSetObj) Dim I As Integer For I = 0 To DataSetObj.Tables(0).Rows.Count - 1 ReDim Preserve Entries(I + 1) Entries(I) = DataSetObj.Tables(0).Rows(I).ItemArray(1) Next Connection.Close() GetEntries = Entries End Function
  • 14. Tản mạn chuyện hậu trường…
  • 15. Tản mạn chuyện hậu trường… WSDL: Web Service Description Language Dùng để mô tả các thông tin về Web Service: Tên phương thức, các tham số đầu vào, kiểu trả về… Ví dụ: File WSDL tương ứng với GetAge <WebMethod()> Public Function GetAge(ByVal Month As Integer, ByVal Day As Integer, ByVal Year As Integer) As Integer …… . End Function
  • 16. Tản mạn chuyện hậu trường… SOAP: Simple Object Access Protocol protocol for exchanging XML-based messages over computer network Ví dụ thông điệp gửi đi từ Client <soap:Envelope xmlns:soap=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;> <soap:Body> <getProductDetails xmlns=&quot;http://warehouse.example.com/ws&quot;> <productID>827635</productID> </getProductDetails> </soap:Body> </soap:Envelope> Ví dụ thông điệp gửi về từ Server <soap:Envelope xmlns:soap=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;> <soap:Body> <getProductDetailsResponse xmlns=&quot;http://warehouse.example.com/ws&quot;> <getProductDetailsResult> <productName>Toptimate 3-Piece Set</productName> <productID>827635</productID> <description>3-Piece luggage set. Black Polyester.</description> <price>96.50</price> <inStock>true</inStock> </getProductDetailsResult> </getProductDetailsResponse> </soap:Body> </soap:Envelope>
  • 17. Tản mạn chuyện hậu trường… UDDI: Universal Description Discovery Integration a “yellow pages” for web services that exist worldwide
  • 18. Q & A… Cám ơn các bạn đã theo dõi