JSONHelper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
using System.Net.Http.Formatting;
using Newtonsoft.Json.Linq;
using log4net;
public enum HttpVerb
{
GET,
POST,
PUT,
DELETE
}
public enum enumStatus
{
SUCCESS,
FAILED,
EXCEPTION,
LOGINID_EXISTS,
WRG_OLD_PWD,
EMAIL_ALREADY_VERIFIED
};
namespace DAL
{
public class Login
{
public string CompanyID { get; set; }
public string LoginID { get; set; }
public string Password { get; set; }
}
public class Password
{
public int StudentSK { get; set; }
public string LoginID { get; set; }
public string OldPassword { get; set; }
public string NewPassword { get; set; }
}
public class NotifyOption
{
public string Key { get; set; }
public string Value { get; set; }
}
public class JSONHelper
{
readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
//Get Method
public string JSONGet(string url)
{
logger.Debug("debug started in JSON Helper");
WebClient webclient = null;
var result = "";
try
{
webclient = new WebClient();
result = webclient.DownloadString(url);
return result;
}
catch (Exception ex)
{
logger.Error("<b>Exception was : </b>" + ex.Message);
logger.Error("<b>Stack Trace : </b>" + ex.StackTrace);
throw ex;
}
finally
{
webclient = null;
result = null;
}
}
public string JSONGetString(string url)
{
logger.Debug("debug started in JSON Helper");
var result = "";
try
{
var outputHtml = string.Empty;
using (WebClient client = new WebClient())
{
client.Encoding = Encoding.UTF8;
outputHtml = client.DownloadString(url);
}
return outputHtml;
}
catch (Exception ex)
{
logger.Error("<b>Exception was : </b>" + ex.Message);
logger.Error("<b>Stack Trace : </b>" + ex.StackTrace);
throw ex;
}
finally
{
result = null;
}
}
//Post Method
public string JSONPost(string url, List<string> param)
{
HttpClient client = null;
Login loginparams = null;
try
{
using (client = new HttpClient())
{
loginparams = new Login { CompanyID = param[1], LoginID = param[2], Password = param[3] };
client.BaseAddress = new Uri(url);
var response = client.PostAsJsonAsync(param[0], loginparams).Result;
if (response.IsSuccessStatusCode)
{
var postlogindetails = response.Content.ReadAsStringAsync().Result;
return postlogindetails;
}
return null;
}
}
catch (Exception ex)
{
logger.Error("<b>Exception was : </b>" + ex.Message);
logger.Error("<b>Stack Trace : </b>" + ex.StackTrace);
throw ex;
}
finally
{
client = null;
loginparams = null;
}
}
//Post method with json object as parameter
public string JSONPost(string url, JObject objJSONPost)
{
HttpClient client = null;
var varPostDetails = "";
JObject objJson = null;
string strStatus = "";
try
{
using (client = new HttpClient())
{
var response = client.PostAsJsonAsync(url, objJSONPost).Result;
if (response.IsSuccessStatusCode)
{
varPostDetails = response.Content.ReadAsStringAsync().Result;
objJson = JObject.Parse(varPostDetails);
if (objJson.SelectToken("ServerMsg.Msg") == null)
strStatus = objJson.SelectToken("Msg").ToString();
else
strStatus = objJson.SelectToken("ServerMsg.Msg").ToString();
if (strStatus.Equals(enumStatus.SUCCESS.ToString()))
{
return varPostDetails;
}
if (strStatus.Equals(enumStatus.LOGINID_EXISTS.ToString()))
{
return varPostDetails;
}
if (strStatus.Equals(enumStatus.FAILED.ToString()))
{
return varPostDetails;
}
else if (strStatus.Equals(enumStatus.EXCEPTION.ToString()))
{
logger.Error("Exception Thrown" + response.RequestMessage);
return enumStatus.EXCEPTION.ToString();
}
}
return null;
}
}
catch (Exception ex)
{
logger.Error("<b>Exception was : </b>" + ex.Message);
logger.Error("<b>Stack Trace : </b>" + ex.StackTrace);
throw ex;
}
finally
{
client = null;
varPostDetails = null;
objJson = null;
}
}
//put method
public string JSONPut(string url, List<string> param)
{
HttpClient client = null;
Password PwdParams = null;
try
{
using (client = new HttpClient())
{
PwdParams = new Password { StudentSK = 1, LoginID = param[1], OldPassword = param[2], NewPassword = param[3] };
client.BaseAddress = new Uri(url);
var response = client.PutAsJsonAsync(param[0], PwdParams).Result;
if (response.IsSuccessStatusCode)
{
var putdetails = response.Content.ReadAsStringAsync().Result;
return putdetails;
}
return null;
}
}
catch (Exception ex)
{
logger.Error("<b>Exception was : </b>" + ex.Message);
logger.Error("<b>Stack Trace : </b>" + ex.StackTrace);
throw ex;
}
finally
{
client = null;
PwdParams = null;
}
}
//Put method with json object as parameter
public string JSONPut(string url, JObject objJsonPut)
{
HttpClient client = null;
var varPutDetails = "";
JObject objJson = null;
string strStatus = "";
try
{
using (client = new HttpClient())
{
client.BaseAddress = new Uri(url);
var response = client.PutAsJsonAsync(url, objJsonPut).Result;
if (response.IsSuccessStatusCode)
{
varPutDetails = response.Content.ReadAsStringAsync().Result;
objJson = JObject.Parse(varPutDetails);
if (objJson.SelectToken("ServerMsg.Msg") == null)
strStatus = objJson.SelectToken("Msg").ToString();
else
strStatus = objJson.SelectToken("ServerMsg.Msg").ToString();
if (strStatus.Equals(enumStatus.SUCCESS.ToString()))
{
strStatus = objJson.SelectToken("Msg").ToString();
}
else if (strStatus.Equals(enumStatus.FAILED.ToString()))
{
return "You'r Email Address is not verified";
}
else if (strStatus.Equals(enumStatus.EXCEPTION.ToString()))
{
return "You'r Email Address is Already Verified";
}
}
//return null;
return varPutDetails;
}
}
catch (Exception ex)
{
logger.Error("<b>Exception was : </b>" + ex.Message);
logger.Error("<b>Stack Trace : </b>" + ex.StackTrace);
throw ex;
}
finally
{
client = null;
varPutDetails = "";
objJson = null;
strStatus = "";
}
}
public bool JSONDelete(string url, int id)
{
HttpClient client;
try
{
using (client = new HttpClient())
{
client.BaseAddress = new Uri(url + id);
var response = client.DeleteAsync(client.BaseAddress).Result;
if (response.IsSuccessStatusCode)
{
return true;
}
else
{
return false;
}
}
}
catch (Exception ex)
{
logger.Error("<b>Exception was : </b>" + ex.Message);
logger.Error("<b>Stack Trace : </b>" + ex.StackTrace);
throw ex;
}
finally
{
client = null;
}
}
}
}
No comments:
Post a Comment