Calling Web API Using HttpWebRequest In C#

Calling Web API Using HttpWebRequest In C#

 Calling Web API Using HttpWebRequest In C#

Step 1

Create Asp.Net Project.

Step 2

Add Web Form GetAreaPostOffice.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GetAreaPostOffice.aspx.cs" Inherits="GetPostOfficeNameByPinCode.GetAreaPostOffice" %>  
  
<!DOCTYPE html>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title></title>  
</head>  
<body>  
    <form id="form1" runat="server">  
        <div>  
            <table>  
                <tr>  
                    <td>  
                        <asp:Label ID="lblPinCode" Text="Area Pin Code : " runat="server"></asp:Label>  
                    </td>  
                    <td>  
                        <asp:TextBox ID="txtPinCode"  runat="server" Width="200" MaxLength="6" onkeypress='return event.charCode >= 48 && event.charCode <= 57'></asp:TextBox>  
                    </td>  
                    <td>  
                        <asp:Button ID="btnSearch" Text="Search" runat="server" OnClick="btnSearch_Click" Enabled="true" />  
                    </td>  
                </tr>  
                <tr>  
                    <td colspan="5">  
                        <asp:Label ID="lblMessage" Text="" runat="server"></asp:Label>  
                    </td>  
                </tr>  
  
            </table>  
        </div>  
        <div>  
            <asp:GridView ID="grdAreaPostOffice" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">  
                <AlternatingRowStyle BackColor="White" />  
                <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />  
                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />  
                <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />  
                <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />  
                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />  
                <SortedAscendingCellStyle BackColor="#FDF5AC" />  
                <SortedAscendingHeaderStyle BackColor="#4D0000" />  
                <SortedDescendingCellStyle BackColor="#FCF6C0" />  
                <SortedDescendingHeaderStyle BackColor="#820000" />  
            </asp:GridView>  
        </div>  
    </form>  
</body>  
</html>

Step 3

Write the code in the code behind file like this.

using LitJson;  
using System;  
using System.Collections.Generic;  
using System.IO;  
using System.Linq;  
using System.Net;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
  
namespace GetPostOfficeNameByPinCode  
{  
    public partial class GetAreaPostOffice : System.Web.UI.Page  
    {  
        protected void Page_Load(object sender, EventArgs e)  
        {  
  
        }  
  
        protected void btnSearch_Click(object sender, EventArgs e)  
        {  
            PostOfficeResult postOfficeResult = null;  
            try  
            {  
                string userAuthenticationURI = "http://postalpincode.in/api/pincode/" + txtPinCode.Text.Trim();  
  
                if (!string.IsNullOrEmpty(userAuthenticationURI))  
                {  
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(userAuthenticationURI);  
                    request.Method = "GET";  
                    request.ContentType = "application/json";  
                    WebResponse response = request.GetResponse();  
                    using (var reader = new StreamReader(response.GetResponseStream()))  
                    {  
                        var ApiStatus = reader.ReadToEnd();  
                        JsonData data = JsonMapper.ToObject(ApiStatus);  
                        string status = data["Status"].ToString();  
                        if (status.ToLower() == "success")  
                        {  
                            postOfficeResult = JsonMapper.ToObject<PostOfficeResult>(ApiStatus);  
  
                        }  
                        if (postOfficeResult != null)  
                        {  
                            grdAreaPostOffice.DataSource = postOfficeResult.PostOffice;  
                            grdAreaPostOffice.DataBind();  
                        }  
                        else  
                        {  
                            lblMessage.Text = data["Message"].ToString();  
                        }  
                    }  
                }  
            }  
            catch  
            {  
            }  
        }  
    }  
}
Next Recommended Reading Consume Web API Using WebClient In C#