Calling Web API Using HttpWebRequest In C#

 

Step 1

Create Asp.Net Project.
 
Step 2

Add Web Form GetAreaPostOffice.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GetAreaPostOffice.aspx.cs" Inherits="GetPostOfficeNameByPinCode.GetAreaPostOffice" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.         <div>  
  12.             <table>  
  13.                 <tr>  
  14.                     <td>  
  15.                         <asp:Label ID="lblPinCode" Text="Area Pin Code : " runat="server"></asp:Label>  
  16.                     </td>  
  17.                     <td>  
  18.                         <asp:TextBox ID="txtPinCode"  runat="server" Width="200" MaxLength="6" onkeypress='return event.charCode >= 48 && event.charCode <= 57'></asp:TextBox>  
  19.                     </td>  
  20.                     <td>  
  21.                         <asp:Button ID="btnSearch" Text="Search" runat="server" OnClick="btnSearch_Click" Enabled="true" />  
  22.                     </td>  
  23.                 </tr>  
  24.                 <tr>  
  25.                     <td colspan="5">  
  26.                         <asp:Label ID="lblMessage" Text="" runat="server"></asp:Label>  
  27.                     </td>  
  28.                 </tr>  
  29.   
  30.             </table>  
  31.         </div>  
  32.         <div>  
  33.             <asp:GridView ID="grdAreaPostOffice" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">  
  34.                 <AlternatingRowStyle BackColor="White" />  
  35.                 <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />  
  36.                 <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />  
  37.                 <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />  
  38.                 <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />  
  39.                 <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />  
  40.                 <SortedAscendingCellStyle BackColor="#FDF5AC" />  
  41.                 <SortedAscendingHeaderStyle BackColor="#4D0000" />  
  42.                 <SortedDescendingCellStyle BackColor="#FCF6C0" />  
  43.                 <SortedDescendingHeaderStyle BackColor="#820000" />  
  44.             </asp:GridView>  
  45.         </div>  
  46.     </form>  
  47. </body>  
  48. </html>  
Step 3

Write the code in the code behind file like this.
  1. using LitJson;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.IO;  
  5. using System.Linq;  
  6. using System.Net;  
  7. using System.Web;  
  8. using System.Web.UI;  
  9. using System.Web.UI.WebControls;  
  10.   
  11. namespace GetPostOfficeNameByPinCode  
  12. {  
  13.     public partial class GetAreaPostOffice : System.Web.UI.Page  
  14.     {  
  15.         protected void Page_Load(object sender, EventArgs e)  
  16.         {  
  17.   
  18.         }  
  19.   
  20.         protected void btnSearch_Click(object sender, EventArgs e)  
  21.         {  
  22.             PostOfficeResult postOfficeResult = null;  
  23.             try  
  24.             {  
  25.                 string userAuthenticationURI = "http://postalpincode.in/api/pincode/" + txtPinCode.Text.Trim();  
  26.   
  27.                 if (!string.IsNullOrEmpty(userAuthenticationURI))  
  28.                 {  
  29.                     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(userAuthenticationURI);  
  30.                     request.Method = "GET";  
  31.                     request.ContentType = "application/json";  
  32.                     WebResponse response = request.GetResponse();  
  33.                     using (var reader = new StreamReader(response.GetResponseStream()))  
  34.                     {  
  35.                         var ApiStatus = reader.ReadToEnd();  
  36.                         JsonData data = JsonMapper.ToObject(ApiStatus);  
  37.                         string status = data["Status"].ToString();  
  38.                         if (status.ToLower() == "success")  
  39.                         {  
  40.                             postOfficeResult = JsonMapper.ToObject<PostOfficeResult>(ApiStatus);  
  41.   
  42.                         }  
  43.                         if (postOfficeResult != null)  
  44.                         {  
  45.                             grdAreaPostOffice.DataSource = postOfficeResult.PostOffice;  
  46.                             grdAreaPostOffice.DataBind();  
  47.                         }  
  48.                         else  
  49.                         {  
  50.                             lblMessage.Text = data["Message"].ToString();  
  51.                         }  
  52.                     }  
  53.                 }  
  54.             }  
  55.             catch  
  56.             {  
  57.             }  
  58.         }  
  59.     }  
  60. }  
Ebook Download
View all
Learn
View all