using System; using System.IO; using System.IO.Compression; using System.Collections.Generic; using System.Linq; using System.Security.Principal; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Threading; using Microsoft.Web.Administration; using System.Management; using System.Diagnostics; using Vrh.XmlProcessing; using System.Xml.Linq; using Vrh.Log4Pro.MaintenanceConsole.ColorConsoleNS; using VRH.Common; using Microsoft.Win32; using System.Reflection; using Vrh.Log4Pro.MaintenanceConsole.CommandLineParserNS; using System.Net.Http; using System.Net.Http.Headers; using System.Collections.Generic; using Newtonsoft.Json; namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS { public static class HttpTools { public enum RequestType { GET,POST,} public static ReturnInfoJSON GetReturninfoJSON(string url, RequestType mode = RequestType.GET, List dictparameterlist=null) { string returninfojsonstring = ""; try { return mode == RequestType.GET? _GetReturninfoJSON(url,out returninfojsonstring) : _PostReturninfoJSON(url, out returninfojsonstring, dictparameterlist); } catch (Exception ex) { return new ReturnInfoJSON() { ReturnValue = -1, ReturnMessage = ex.Message + "\n" + returninfojsonstring, }; } } private static ReturnInfoJSON _GetReturninfoJSON(string url, out string returninfojsonstring) { var returninfojsonstream = Task.Run(async () => await (new HttpClient()).GetStreamAsync(url)).GetAwaiter().GetResult(); returninfojsonstring = GetStreamAsString(returninfojsonstream); ReturnInfoJSON returninfojson = (ReturnInfoJSON)JsonConvert.DeserializeObject(returninfojsonstring,typeof(ReturnInfoJSON)); //ReturnInfoJSON returninfojson = Task.Run(async () => await JsonSerializer.DeserializeAsync(returninfojsonstream)).GetAwaiter().GetResult(); return returninfojson; } private static ReturnInfoJSON _PostReturninfoJSON(string url, out string returninfojsonstring, List dictparameterlist = null) { var jsonstring = JsonConvert.SerializeObject(dictparameterlist.Where(p => p.PassTo == Vrh.XmlProcessing.UrlElement.ParameterTypes.dict)); HttpContent requestcontent = new StringContent(jsonstring); var returninfojsonhttpresponsemessage = Task.Run(async () => await (new HttpClient()).PostAsync(url, requestcontent)).GetAwaiter().GetResult(); var returninfojsonstream = Task.Run(async () => await returninfojsonhttpresponsemessage.Content.ReadAsStreamAsync()).GetAwaiter().GetResult(); returninfojsonstring = GetStreamAsString(returninfojsonstream); ReturnInfoJSON returninfojson = (ReturnInfoJSON)JsonConvert.DeserializeObject(returninfojsonstring, typeof(ReturnInfoJSON)); //var returninfojson = Task.Run(async () => await JsonSerializer.DeserializeAsync(returninfojsonstream)).GetAwaiter().GetResult(); return returninfojson; } private static string GetStreamAsString(Stream stream) { StreamReader reader = new StreamReader(stream); return reader.ReadToEnd(); byte[] bytes = new byte[stream.Length]; stream.Position = 0; stream.Read(bytes, 0, (int)stream.Length); return Encoding.ASCII.GetString(bytes); // this is your string } public class ReturnInfoJSON { public int ReturnValue { get; set; } public string ReturnMessage { get; set; } } } }