Core ASP .net C# API creation with request and response

1. API Call:

—————————-

using API_Hall.CommonLayer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

—————————-

String qs = “?roll=” + Roll + “&password=” + _Password + “&hall=” + Hall + “&from=” + From + “&to=” + To + “&option=” + Option;
//Response.Redirect(“API_Call.aspx” + qs);
//lblMsg.Text = “Success !!”;

HttpClient client = new HttpClient();
//client.BaseAddress = new Uri(“http://localhost:2089/API_Call.aspx” + qs);
//client.BaseAddress = new Uri(Request.Url.OriginalString.Replace(Request.Url.PathAndQuery, “”) + Request.ApplicationPath.ToString() + “/API_Call.aspx” + qs);
client.BaseAddress = new Uri(Request.Url.OriginalString.Replace(Request.Url.PathAndQuery, “”) + Request.ApplicationPath.ToString() + “API_Call.aspx” + qs);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(“application/json”));
HttpResponseMessage response = client.GetAsync(qs).Result;

Int32 OperationSuccessFlag = 0;
if (response.Headers.Contains(“<replay>1</replay>”) || response.ReasonPhrase.Contains(“<replay>1</replay>”))
{
OperationSuccessFlag = 1;
}
else if (response.Headers.Contains(“<replay>0</replay>”) || response.ReasonPhrase.Contains(“<replay>0</replay>”))
{
OperationSuccessFlag = 0;
}

try
{
if (response.IsSuccessStatusCode) //(response.IsSuccessStatusCode == true)
{
if (response.ReasonPhrase.Contains(“<replay>1</replay>”))
{
lblMsg.Text = “Successful !!”;
}
}
else
{
if (response.ReasonPhrase.Contains(“<replay>0</replay>”))
{
lblMsg.Text = “Failed !!”;
}
}
}
catch (Exception excp) { }

2. API Creation:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Json;
using System.Net;
using System.Web.Http;
using System.IO;
using System.Text;
using System.Web.Script.Serialization;

namespace API_Hall
{
public partial class API_Call : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
//  String qs = “?roll=” + Roll + “&password=” + _Password + “&hall=” + Hall + “&from=” + From + “&to=” + To + “&option=” + Option;
String Roll = Request.QueryString[“roll”].ToString().Trim();
String Password = Request.QueryString[“password”].ToString().Trim();
String Hall = Request.QueryString[“hall”].ToString().Trim();
String From = Request.QueryString[“from”].ToString().Trim();
String To = Request.QueryString[“to”].ToString().Trim();
String Option = Request.QueryString[“option”].ToString().Trim();

Int32 IsSuccess = 0;
//////////////////////////////////// Operational Code /////////////////////////

//////////////////////////////////////////////////////////////////////////////

if (IsSuccess == 1)
{
//string[] myarr = { “<replay>1</replay>” };
//JavaScriptSerializer serializer = new JavaScriptSerializer();
//string arrayJson = serializer.Serialize(myarr);

//Response.Write(“<replay>0</replay>”);

Response.AddHeader(“Headers”, “<replay>1</replay>”);
Response.AppendHeader(“Headers”, “<replay>1</replay>”);
//Response.ContentType = “xml”;
Response.StatusCode = 200;
//Response.Status = “OK”;
Response.StatusDescription = “<replay>1</replay>”; //this set ReasonPhrase
Response.Clear();
}
else
{
Response.AddHeader(“Headers”, “<replay>0</replay>”);
Response.AppendHeader(“Headers”, “<replay>0</replay>”);
//Response.ContentType = “xml”;
Response.StatusCode = 200;
Response.StatusDescription = “<replay>0</replay>”; //this set ReasonPhrase
Response.Clear();
}
}
catch (Exception ex) { }
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s