// // PostJson.cs // // Author: // Paul Schneider // // Copyright (c) 2015 Paul Schneider // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License // along with this program. If not, see . using System.Net; using System.IO; using System.Threading.Tasks; using Newtonsoft.Json; using System; using System.Json; namespace Yavsc.Helpers { /// /// Simple json post method. /// public class SimpleJsonPostMethod : IDisposable { private HttpWebRequest request=null; /// /// Initializes a new instance of the Yavsc.Helpers.SimpleJsonPostMethod class. /// /// Path to method. public SimpleJsonPostMethod (string pathToMethod, string authorizationHeader = null) { request = (HttpWebRequest) WebRequest.Create (pathToMethod); request.Method = "POST"; request.Accept = "application/json"; request.ContentType = "application/json"; request.SendChunked = true; request.TransferEncoding = "UTF-8"; if (authorizationHeader!=null) request.Headers["Authorization"]=authorizationHeader; } public void Dispose() { request.Abort(); } /// /// Invoke the specified query. /// /// Query. public TAnswer Invoke(object query) { using (Stream streamQuery = request.GetRequestStream()) { using (StreamWriter writer = new StreamWriter(streamQuery)) { writer.Write (JsonConvert.SerializeObject(query)); }} TAnswer ans = default (TAnswer); using (WebResponse response = request.GetResponse ()) { using (Stream responseStream = response.GetResponseStream ()) { using (StreamReader rdr = new StreamReader (responseStream)) { ans = (TAnswer) JsonConvert.DeserializeObject (rdr.ReadToEnd ()); } } response.Close(); } return ans; } public async Task InvokeJson(object query) { JsonValue jsonDoc=null; using (Stream streamQuery = request.GetRequestStream()) { using (StreamWriter writer = new StreamWriter(streamQuery)) { writer.Write (JsonConvert.SerializeObject(query)); }} using (WebResponse response = request.GetResponse ()) { using (Stream stream = response.GetResponseStream ()) { if (stream.Length>0) jsonDoc = await Task.Run (() => JsonObject.Load (stream)); } response.Close(); } return jsonDoc; } } }