Server Side API wrapper for Cross Domain Requests
From Logic Wiki
We don't need to de-serialize and serialize json objects. Instead I prefer to read from API as string and change it to camelCase then publish it via web server.
We don't need models or json serializations for this method.
using System.IO;
using System.Net;
using System.Web;
using System.Web.Services;
namespace Optevia.Portal.OPSE.WebServices
{
/// <summary>
/// Summary description for CouncilAppsService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class CouncilAppsService : System.Web.Services.WebService
{
string GetPageSource(string url)
{
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
webrequest.Method = "GET";
HttpWebResponse webResponse = (HttpWebResponse)webrequest.GetResponse();
string responseHtml;
using (StreamReader responseStream = new StreamReader(webResponse.GetResponseStream()))
{
responseHtml = responseStream.ReadToEnd().Trim();
}
return responseHtml;
}
[WebMethod]
public void GetBinRoundsText(string UPRN, string SearchDate)
{
HttpContext.Current.Response.Write("binresults(" +
GetPageSource("http://applications.rochdale.gov.uk/BinCollectionWebAPI/api/Home/GetBinRounds?UPRN=" +
UPRN + "&SearchDate=" + SearchDate).Replace("UPRN", "uprn").Replace("RoundDescription", "roundDescription").Replace("WorkpackID", "workpackID").Replace("StartDate", "startDate").Replace("PostCode", "postCode").Replace("CollectionType", "collectionType").Replace("DayOfWeek", "dayOfWeek").Replace("WorkpackName","workpackName") + ");");
}
[WebMethod]
public void GetCouncillorsText(string postcode)
{
HttpContext.Current.Response.Write("represults(" +
GetPageSource("http://applications.rochdale.gov.uk/MyCouncillorsWebAPI/Councillor?PostCode=" + postcode).Replace("UID", "uid").Replace("WardID", "wardID").Replace("Name", "name").Replace("Title", "title").Replace("Ward", "ward").Replace("Photo", "photo").Replace("ConvertedUID", "convertedUID").Replace("ALTText", "altText").Replace("Party", "party") + ");");
}
}
}