Consuming Web Service
Web service is a communication platform between application. Web service hosted by the same application is also can we consume by
the application itself . The most common communication protocol of Web Services SOAP.SOAP stand for simple object access protocol.
Web service can be consume in two way. One can consumer service by
Creating proxy
Before using web proxy, a proxy must be created. The proxy is registered with the client application , the client application make call to the web service as it is calling local method. When the proxy is called, that the WebService is wrapped with proper method with SOAP protocol, on the Response ,proxy decode the result and return it to the application.
A proxy can be added from
Project >>AddServiceReference>>Address>> Go>>OK.
You will find service reference icon , it is displays on the same solution Explorer. In the below example we are using the WebService of the previous chapter. You will enter first name and last name and your WebService will return the complete name.
1)AddServiceReference
Web service is a communication platform between application. Web service hosted by the same application is also can we consume by
the application itself . The most common communication protocol of Web Services SOAP.SOAP stand for simple object access protocol.
- SOAP XML based protocol for accessing web service
- SOAP communicate over HTTP
- SOAP is W3C recommended.
Web service can be consume in two way. One can consumer service by
- Creating proxy
- Consuming web service programmatically
Creating proxy
Before using web proxy, a proxy must be created. The proxy is registered with the client application , the client application make call to the web service as it is calling local method. When the proxy is called, that the WebService is wrapped with proper method with SOAP protocol, on the Response ,proxy decode the result and return it to the application.
A proxy can be added from
Project >>AddServiceReference>>Address>> Go>>OK.
You will find service reference icon , it is displays on the same solution Explorer. In the below example we are using the WebService of the previous chapter. You will enter first name and last name and your WebService will return the complete name.
1)AddServiceReference
2)Service Reference icon
3) Calling Page HTML
<%@
Page
Language="C#"
AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="_Default"
%>
<!DOCTYPE
html
PUBLIC
"-//W3C//DTD
XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml">
<head
runat="server">
<title></title>
</head>
<body>
<form
id="form1"
runat="server">
<table>
<tr>
<td>First
Name :
</td>
<td>
<asp:TextBox
ID="txtFirstName"
runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Last
Name :</td>
<td>
<asp:TextBox
ID="txtLastName"
runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td
colspan="2">
<asp:Button
ID="Button1"
runat="server"
Text="Button"
OnClick="Button1_Click"
/>
</td>
</tr>
</table>
<div>
</div>
</form>
</body>
</html>
3) Calling Page C# Code
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
public
partial
class
_Default
: System.Web.UI.Page
{
protected
void
Page_Load(object
sender, EventArgs
e)
{
}
protected
void
Button1_Click(object
sender, EventArgs
e)
{
WebService
ab = new
WebService();
string
fullName=ab.GetFullName(txtFirstName.Text.ToString(),
txtLastName.Text.ToString());
Response.Write(fullName);
}
}
5) Calling Page HTML View
6) Calling Page , After WebService Call ,Display Full Name
Another way to consume WebService is programmatically . First you have to create request with WebService URL . After response is created to get response from webservice. SteamReader and Steam is created to read web service response. The web service we are using return JSON format.
1) C# code to consume WebService
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Net;
using
System.Text;
using
System.IO;
public
partial
class
_Default
: System.Web.UI.Page
{
protected
void
Page_Load(object
sender, EventArgs
e)
{
}
protected
void
Button1_Click(object
sender, EventArgs
e)
{
string
WebServiceUrl = "Your
Web Service URL";
WebRequest
wreq = WebRequest.Create(WebServiceUrl);
HttpWebResponse
wres = (HttpWebResponse)wreq.GetResponse();
Stream
dtst = wres.GetResponseStream();
StreamReader
sr = new
StreamReader(dtst);
string
responseFromServer = sr.ReadToEnd();
sr.Close();
dtst.Close();
wres.Close();
}
}
{ "RestResponse" : { "messages" : [ "Country found matching code [PK]." ], "result" : { "name" : "Pakistan", "PK005" : "PK", "PK003" : "PAK" } } }
No comments:
Post a Comment