using System; using System.Collections.Specialized; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Web; using System.Xml.Serialization; using Newtonsoft.Json; /* The App requires References to System.Web to use function HttpUtility.UrlEncode and Requires external reference Newtonsoft.Json to use function JsonConvert.SerializeObject, to install it use NuGet Package Manager, https://www.nuget.org/packages/newtonsoft.json/ Install-Package Newtonsoft.Json */ namespace ConsoleApplication1 { public class Program { public class PRequest { public string Account { get; set; } public string ApiKey { get; set; } public string IdDevice { get; set; } public string StartUTC { get; set; } public string EndUTC { get; set; } public bool LastLocation { get; set; } // Only for query GetLocation public bool LastAlert { get; set; } // Only for query GetAlert public PRequest() { Account = "user@plaspy.com"; ApiKey = "MyAPIKey"; IdDevice = "IdDevice"; LastPosition = true; StartUTC = DateTime.UtcNow.AddHours(-12).ToString("O"); EndUTC = DateTime.UtcNow.ToString("O"); } public string GetParamsRequest() { NameValueCollection request = new NameValueCollection(); request["Account"] = Account; request["ApiKey"] = ApiKey; request["IdDevice"] = IdDevice; request["StartUTC"] = StartUTC; request["EndUTC"] = EndUTC; request["LastPosition"] = LastPosition.ToString(); return string.Join("&", request.Cast().Select(e => e + "=" + HttpUtility.UrlEncode(request[e]))); } public string GetXMLRequest() { StringBuilder builder = new StringBuilder(); XmlSerializer s = new XmlSerializer(this.GetType()); using (StringWriter writer = new StringWriter(builder)) { s.Serialize(writer, this); } return builder.ToString().Replace("utf-16", "utf-8"); } public string GetJSONRequest() { return JsonConvert.SerializeObject(this); } } public const string APIURL = "https://api.plaspy.com/api/"; private static string GetPOSTResponse(string operation, string contentType, string data) { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(APIURL + operation); request.Method = "POST"; request.ContentType = contentType + ";charset=utf-8"; UTF8Encoding encoding = new UTF8Encoding(); byte[] bytes = encoding.GetBytes(data); request.ContentLength = bytes.Length; using (Stream requestStream = request.GetRequestStream()) { // Send the data. requestStream.Write(bytes, 0, bytes.Length); } WebResponse webResponse = request.GetResponse(); //get result and release source using (StreamReader reader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8)) { return reader.ReadToEnd(); } } private static string GetGetResponse(string operation, string contentType, string data) { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(APIURL + operation + "?" + data); request.Method = "GET"; request.ContentType = contentType + ";charset=utf-8"; WebResponse webResponse = request.GetResponse(); //get result and release source using (StreamReader reader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8)) { return reader.ReadToEnd(); } } private static void MakeRequest(string operation, string message, string contentType, string request, bool post) { Console.WriteLine(message); Console.WriteLine("Request: {0}", request); string response = post ? GetPOSTResponse(operation, contentType, request) : GetGetResponse(operation, contentType, request); Console.WriteLine("Response: {0}", response); Console.WriteLine(); } private static void MakePostRequest(string operation, string message, string contentType, string request) { MakeRequest(operation, message, contentType, request, true); } private static void MakeGetRequest(string operation, string message, string contentType, string request) { MakeRequest(operation, message, contentType, request, false); } static void Main(string[] args) { PRequest request = new PRequest(); Console.WriteLine("GetLocation"); MakePostRequest("GetLocation", "Testing POST", "application/x-www-form-urlencoded", request.GetParamsRequest()); MakePostRequest("GetLocation", "Testing POST XML", "text/xml", request.GetXMLRequest()); MakePostRequest("GetLocation", "Testing POST JSON", "application/json", request.GetJSONRequest()); MakeGetRequest("GetLocation", "Testing Get JSON", "application/json", request.GetParamsRequest()); Console.WriteLine("GetAlert"); MakePostRequest("GetAlert", "Testing POST", "application/x-www-form-urlencoded", request.GetParamsRequest()); MakePostRequest("GetAlert", "Testing POST XML", "text/xml", request.GetXMLRequest()); MakePostRequest("GetAlert", "Testing POST JSON", "application/json", request.GetJSONRequest()); MakeGetRequest("GetAlert", "Testing Get JSON", "application/json", request.GetParamsRequest()); } } }