using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
namespace AjayApi.Models
{
public class ApiCall
{
//public clsService()
//{
// ServiceUrl = "https://api.xxxx.xxx/api";
//}
public ApiCall(string strUrl)
{
ServiceUrl = strUrl;
}
readonly string ServiceUrl;
public string strMethodName { get; set; }
public string strMethodArgument { get; set; }
public string strMethodType { get; set; }
public string RestServiceResponce()
{
string strResult = string.Empty;
try
{
Uri objUrl = new Uri(string.Format("{0}/{1}", ServiceUrl, strMethodName));
HttpWebRequest objRequest = WebRequest.Create(objUrl) as HttpWebRequest;
objRequest.Method =
string.IsNullOrWhiteSpace(strMethodType) ? "GET" : strMethodType;
objRequest.ContentType = "application/json";
WebProxy objWebProxy = new WebProxy();
objWebProxy.IsBypassed(objUrl);
objRequest.Proxy = objWebProxy;
if (!string.IsNullOrEmpty(strMethodArgument))
{
try
{
using (var streamWriter = new StreamWriter(objRequest.GetRequestStream()))
{
streamWriter.Write(strMethodArgument);
}
}
catch { }
}
using (HttpWebResponse objResponce =
objRequest.GetResponse() as HttpWebResponse)
{
StreamReader objReader =
new StreamReader(objResponce.GetResponseStream());
strResult = objReader.ReadToEnd();
objRequest.KeepAlive = false;
objResponce.Close();
}
}
catch (Exception ex) { strResult = ex.Message; }
return strResult;
}
}
}
Get Method Call
using System.Web.Script.Serialization;
JavaScriptSerializer os =
new JavaScriptSerializer() { MaxJsonLength = Int32.MaxValue };
string Baseurl = "http://localhost:51037/";
public ActionResult Index()
{
ApiCall api = new ApiCall("http://localhost:51037/api");
api.strMethodName = "ApiSubject?QueryType=selectSubject&subjectId=1";
// Subjects1 s = new Subjects1();
//===
// Subjectfield ss = new Subjectfield();
// ss.QueryType = "insert";
// ss.SubjectId = "0";
//ss.Subject = "sub00";
//ss.Active = "A";
//ss.Date = "";
//ss.UserId = "1";
//=====
// s.QueryType = "selectSubject";
// s.SubjectId = "1";
// api.strMethodArgument = os.Serialize(s);
//api.strMethodArgument = "{ QueryType = selectSubject, subjectId = 1 }";
api.strMethodType = "GET";
string dd = api.RestServiceResponce();
ViewBag.Title = "Home Page";
//GetApi();
return View();
}
Call Other the "GET" Method
using System.Web.Script.Serialization;
JavaScriptSerializer os =
new JavaScriptSerializer() { MaxJsonLength = Int32.MaxValue };
string Baseurl = "http://localhost:51037/";
public ActionResult Index()
{
ApiCall api = new ApiCall("http://localhost:51037/api");
api.strMethodName = "ApiSubject";
//Subjects1 s = new Subjects1();
//===
Subjectfield ss = new Subjectfield();
ss.QueryType = "insert";
ss.SubjectId = "0";
ss.Subject = "sub00";
ss.Active = "A";
ss.Date = "";
ss.UserId = "1";
//=====
// s.QueryType = "selectSubject";
// s.SubjectId = "1";
api.strMethodArgument = os.Serialize(ss);
//api.strMethodArgument = "{ QueryType = selectSubject, subjectId = 1 }";
api.strMethodType = "POST";
string dd = api.RestServiceResponce();
ViewBag.Title = "Home Page";
//GetApi();
return View();
}
No comments:
Post a Comment