Saturday, January 19, 2008

ASMX Web Service Part 2 - Client

VIn this post, the web service created in Part 1 will be consumed into a web page through Server Side Web Service Integration, Client Side SOAP XML HTTP Post, HTTP Form Post and Get. Following are the steps.

1.       Publish your ASMX Web Service on your local machine under IIS 5.1/6.0/7.0, in this example my published web service is located at my local machine:  http://vishwa/examplewebservice/customerwebservice.asmx
2.       Create a Web Site in Visual Studio 2008/5
3.       Add a Page called CustomerWebServiceTest.aspx, it will automatically added a code behind page CustomerWebServiceTest.aspx.vb
4.       Right Click on your Web Site Project and Add Web Reference by adding the published web service URL, give a local name to this Web Service Reference, in this example I named CustomerWebService.
5.       Add the following code in the  Page
 
CustomerWebServiceTest.aspx : Add following code in this page
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="CustomerWebServiceTest.aspx.vb" Inherits="CustomerWebServiceTest" %>
 
    <!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>Test ASMX Web Service</title>
    </head>
  < <body>   
 
    <h2>Test Customer ASMX Web Service</h2>
   
    <form id="frmCustServer" runat="server">
        <div>
        <table>
         <tr>
            <td colspan="2"><b>Through Server Side Web Service Reference</b></td>
        </tr>
        <tr>
            <td >Customer ID :</td>
            <td><asp:TextBox ID="txtCustID" runat="server" Text="12345"></asp:TextBox></td>
        </tr>     
        <tr>
            <td>Customer Name :</td>
        <td>
            <asp:TextBox ID="txtCustName" runat="server" Text="Joe Smith"></asp:TextBox>
        </td>
        </tr>
        <tr>
            <td>Customer DOB (yyyy-mm-dd):</td>
            <td><asp:TextBox ID="txtCustDOB" runat="server" Text="1980-08-08"></asp:TextBox></td>
        </tr>
        <tr>
            <td>Customer Address:</td>
            <td><asp:TextBox ID="txtCustAddress" runat="server" Text="unknown"></asp:TextBox></td>
        </tr>
          <tr>
            <td><asp:Button ID="btnGetCustomer" runat="server" Text="Get Customer" /></td>
            <td><asp:Button ID="btnAddCustomer" runat="server" Text="Add Customer" /></td>
        </tr>
        <tr>
            <td> <asp:Button ID="btnUpdateCustomer" runat="server" Text="Update Customer" /></td>
            <td><asp:Button ID="btnDeleteCustomer" runat="server" Text="Delete Customer" /></td>
        </tr>
        <tr>
            <td>
            <asp:Button id="btnGetCustomers" runat="server" Text="Get All Customers"/></td>
            <td><asp:Label ID="lblStatus" runat="server" Text="Status" 
                    ForeColor="#CC3300" style="font-weight: 700"></asp:Label></td>
        </tr>
        </table>
   </div>       
   
    </form> 
    
    <br />
    <form id="frmCustomerS" method="post" action="CustomerWebServiceTest.aspx">
    <div>    
        <table>
        <tr><td colspan="2"><b>Through Client Side SOAP/XML HTTP POST</b></td></tr>
        <tr><td>Customer ID :</td><td><input name="ID" id="ID" type="text" value="12345" /> </td></tr>
        <tr><td>Customer Name :</td><td><input name="Name" id="Name" type="text" value="Chris Clark" /> </td></tr>
        <tr><td>Customer DOB (yyyy-mm-dd):</td><td><input name="DOB" id="DOB" type="text" value="1980-08-08"/> </td></tr>
        <tr><td>Customer Address:</td><td><input name="Address" id="Address" type="text" value="unknown"/> </td></tr>
         <tr>
            <td><input type="button" name="btnGetCustomer" value="Get Customer" onclick="GetCustomerSOAP()" /></td>
            <td><input type="button" name="btnAddCustomer" value="Add Customer" onclick="AddCustomerSOAP()" /></td>
        </tr>
         <tr>
            <td><input type="button" name="btnUpdateCustomerS" value="Update Customer" onclick="UpdateCustomerSOAP()" /></td>
            <td><input type="button" name="btnDeleteCustomerS" value="Delete Customer" onclick="DeleteCustomerSOAP()"/></td>
        </tr>     
        <tr>
            <td colspan="2"><input type="button" name="btnGetCustomersS" value="Get All Customers" onclick="GetCustomersSOAP()"/></td>
        </tr>   
        </table>
    </div>   
    </form>
    
    
     <br />
    <form id="frmCustomerPost" method="post" action="http://vishwa/examplewebservice/customerwebservice.asmx/GetCustomer">
    <div>    
        <table>
        <tr><td colspan="2"><b>Through Client Side HTML Form HTTP POST</b></td></tr>
        <tr><td>Customer ID :</td><td><input name="ID" type="text" value="969225896" /> </td></tr>
         <tr>
            <td colspan="2"><input type="submit" name="btnGetAllCustomers" value="Get Customer"/></td>
        </tr>   
        </table>
    </div>   
    </form>
    
    <form id="frmCustomerGet" method="get" action="http://vishwa/examplewebservice/customerwebservice.asmx/GetCustomer">
    <div>    
        <table>
        <tr><td colspan="2"><b>Through Client Side HTML Form HTTP GET</b></td></tr>
        <tr><td>Customer ID :</td><td><input name="ID" type="text" value="969225896" /> </td></tr>
         <tr>
            <td colspan="2"><input type="submit" name="btnGetAllCustomers" value="Get Customer"/></td>
        </tr>   
        </table>
    </div>   
    </form>
    
    <script type="text/javascript" language="javascript">
           
        var urlToPost = "http://vishwa/examplewebservice/customerwebservice.asmx";
        var fixedSoapAction = "http://webservices.vishwamohan.net/";
        var serviceNameSpace = "\"http://webservices.vishwamohan.net\"";
       
        var d = new Date();
        
        function getFullDate()
            {
            if (d.getDate()<10) 
                return "0" + d.getDate(); 
            else 
                return d.getDate();
             } 
             
         function getFullMonth()
            {
            if (d.getMonth()<10) 
                return "0" + parseInt(d.getMonth()+1); 
            else 
                return parseInt(d.getMonth()+1);
             }   
        
        var curdate = d.getFullYear() + "-" + getFullMonth()+ "-" + getFullDate();
         function GetCustomerSOAP() 
            {
              var dataText = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"";
                  dataText = dataText + " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"; 
                  dataText = dataText + " <soap:Body><GetCustomer xmlns=" + serviceNameSpace + ">"; 
                  dataText += " <ID>" + frmCustomerS.ID.value + "</ID>"; 
                  dataText += " </GetCustomer>"; 
                  dataText += " </soap:Body></soap:Envelope>";
                  alert(dataText);
                  
              var xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");                  
                  xmlHttp.open("POST", urlToPost,false);               
                  xmlHttp.setRequestHeader("Content-Type", "text/xml");
                  xmlHttp.setRequestHeader("SOAPAction", fixedSoapAction +"GetCustomer");
                  xmlHttp.send(dataText);
                  alert(xmlHttp.responseText);
            }
            
            function GetCustomersSOAP() 
            {
              var dataText = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"";
                  dataText = dataText + " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"; 
                  dataText = dataText + " <soap:Body><GetCustomers xmlns=" + serviceNameSpace + ">"; 
                  dataText += " </GetCustomers>"; 
                  dataText += " </soap:Body></soap:Envelope>";
                  alert(dataText);
              
              var xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");                  
                  xmlHttp.open("POST", urlToPost,false);               
                  xmlHttp.setRequestHeader("Content-Type", "text/xml");
                  xmlHttp.setRequestHeader("SOAPAction", fixedSoapAction +"GetCustomers");
                  xmlHttp.send(dataText);
                  alert(xmlHttp.responseText);
            }
    
        function AddCustomerSOAP() 
            {
               var dataText = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"";
                  dataText += " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"; 
                  dataText += " <soap:Body><AddCustomer xmlns=" + serviceNameSpace + "> <CustomerRecord>";
                  dataText += " <ID>0</ID><Name>" + frmCustomerS.Name.value + "</Name><DOB>" + frmCustomerS.DOB.value +"</DOB>" ;
                  dataText += " <Address>" + frmCustomerS.Address.value + "</Address>";
                  dataText += " <DateCreated>" + curdate + "</DateCreated><DateModified>" + curdate + "</DateModified>" ;
                  dataText += " </CustomerRecord></AddCustomer>"
                  dataText += " </soap:Body></soap:Envelope>";
                  alert(dataText);
              
              var xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");                  
                  xmlHttp.open("POST", urlToPost,false);               
                  xmlHttp.setRequestHeader("Content-Type", "text/xml");
                  xmlHttp.setRequestHeader("SOAPAction", fixedSoapAction +"AddCustomer");
                  xmlHttp.send(dataText);
                  alert(xmlHttp.responseText);
            }
            
            function UpdateCustomerSOAP() 
            {
               var dataText = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"";
                  dataText += " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"; 
                  dataText += " <soap:Body><UpdateCustomer xmlns=" + serviceNameSpace + "> <CustomerRecord>";
                  dataText += " <ID>" + frmCustomerS.ID.value + "</ID><Name>" + frmCustomerS.Name.value + "</Name><DOB>" + frmCustomerS.DOB.value +"</DOB>" ;
                  dataText += " <Address>" + frmCustomerS.Address.value + "</Address>";
                  dataText += " <DateCreated>" + curdate + "</DateCreated><DateModified>" + curdate + "</DateModified>" ;
                  dataText += " </CustomerRecord></UpdateCustomer>"
                  dataText += " </soap:Body></soap:Envelope>";
                  alert(dataText);
              
              var xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");                  
                  xmlHttp.open("POST", urlToPost,false);               
                  xmlHttp.setRequestHeader("Content-Type", "text/xml");
                  xmlHttp.setRequestHeader("SOAPAction", fixedSoapAction +"UpdateCustomer");
                  xmlHttp.send(dataText);
                  alert(xmlHttp.responseText);
            }
            
            
             function DeleteCustomerSOAP() 
            {
              var dataText = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"";
                  dataText = dataText + " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"; 
                  dataText = dataText + " <soap:Body><DeleteCustomer xmlns=" + serviceNameSpace + ">"; 
                  dataText += " <ID>" + frmCustomerS.ID.value + "</ID>"; 
                  dataText += " </DeleteCustomer>"; 
                  dataText += " </soap:Body></soap:Envelope>";
                  alert(dataText);
              
              var xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");                  
                  xmlHttp.open("POST", urlToPost,false);               
                  xmlHttp.setRequestHeader("Content-Type", "text/xml");
                  xmlHttp.setRequestHeader("SOAPAction", fixedSoapAction +"DeleteCustomer");
                  xmlHttp.send(dataText);
                  alert(xmlHttp.responseText);
            }
    
    </script>
    
    </body>
   </html>
CustomerWebServiceTest.aspx.vb : Add following code in code behind
 
    Partial Class CustomerWebServiceTest
    Inherits System.Web.UI.Page
 
    Protected Sub btnGetCustomer_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGetCustomer.Click
        Dim webSrv As New CustomerWebService.CustomerWebService
        Dim cust As CustomerWebService.Customer = webSrv.GetCustomer(CInt(Me.txtCustID.Text))
        If cust.ID > 0 Then
            Me.txtCustID.Text = cust.ID
            Me.txtCustName.Text = cust.Name
            Me.txtCustDOB.Text = cust.DOB
            Me.txtCustAddress.Text = cust.Address
            Me.lblStatus.Text = "Customer found."
        Else
            Me.lblStatus.Text = "Customer not found."
            Me.txtCustName.Text = ""
            Me.txtCustDOB.Text = ""
            Me.txtCustAddress.Text = ""
            
        End If
    End Sub
 
    Protected Sub btnAddCustomer_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddCustomer.Click
        Dim webSrv As New CustomerWebService.CustomerWebService
        Dim cust As New CustomerWebService.Customer
        cust.Name = Me.txtCustName.Text
        cust.Address = Me.txtCustAddress.Text
        cust.DOB = CDate(Me.txtCustDOB.Text)
        Dim rtn As Integer = webSrv.AddCustomer(cust)
        Me.txtCustID.Text = rtn.ToString
        lblStatus.Text = "Cust ID: " & rtn.ToString
    End Sub
 
    Protected Sub btnUpdateCustomer_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdateCustomer.Click
        Dim webSrv As New CustomerWebService.CustomerWebService
        Dim cust As New CustomerWebService.Customer
        cust.ID = CInt(Me.txtCustID.Text)
        cust.Name = Me.txtCustName.Text
        cust.DOB = CDate(Me.txtCustDOB.Text)
        cust.Address = Me.txtCustAddress.Text
        Dim rtn As Boolean = webSrv.UpdateCustomer(cust)
        lblStatus.Text = "Update Status is : " & rtn.ToString
    End Sub
 
    Protected Sub btnDeleteCustomer_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDeleteCustomer.Click
        Dim webSrv As New CustomerWebService.CustomerWebService
        Dim rtn As Boolean = webSrv.DeleteCustomer(CInt(Me.txtCustID.Text))
        lblStatus.Text = "Delete Status is : " & rtn.ToString
    End Sub
 
    Protected Sub btnGetCustomers_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGetCustomers.Click
        Dim webSrv As New CustomerWebService.CustomerWebService
        Dim custArr() As CustomerWebService.Customer
        custArr = webSrv.GetCustomers()
 
        For Each cust As CustomerWebService.Customer In custArr
            Response.Write("ID: " & cust.ID.ToString & " Name: " & cust.Name & "<br/>")
        Next
        lblStatus.Text = "No of Customer Records found: " & custArr.Length.ToString
    End Sub
   End Class
 
  
Now Run your website and test your service, you should be able to test each process. The page at run time looks like…
 

No comments: