Blame view

Vrh.Log4Pro.MaintenanceConsole/Tools - Http.cs 4.29 KB
5df5f3e5   Schwirg László   v1.14.0.0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  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;
aa2e3807   Schwirg László   v1.15.0
26
  using Newtonsoft.Json;
5df5f3e5   Schwirg László   v1.14.0.0
27
28
29
30
31
32
  
  namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS
  {
      public static class HttpTools
      {
          public enum RequestType { GET,POST,}
c4219940   Schwirg László   WebApplicationMan...
33
          public static ReturnInfoJSON GetReturninfoJSON(string url, int timeoutinseconds,RequestType mode = RequestType.GET, List<Vrh.XmlProcessing.UrlElement.UrlParameter> dictparameterlist=null)
5df5f3e5   Schwirg László   v1.14.0.0
34
35
          {
              string returninfojsonstring = "";
c4219940   Schwirg László   WebApplicationMan...
36
              timeoutinseconds = timeoutinseconds <= 0 ? 100: timeoutinseconds;
5df5f3e5   Schwirg László   v1.14.0.0
37
38
              try
              {
c4219940   Schwirg László   WebApplicationMan...
39
                  return mode == RequestType.GET? _GetReturninfoJSON(url, timeoutinseconds,out returninfojsonstring) : _PostReturninfoJSON(url, timeoutinseconds, out returninfojsonstring, dictparameterlist);
5df5f3e5   Schwirg László   v1.14.0.0
40
41
42
43
              }
              catch (Exception ex) { return new ReturnInfoJSON() { ReturnValue = -1, ReturnMessage = ex.Message + "\n" + returninfojsonstring, }; }
          }
          
c4219940   Schwirg László   WebApplicationMan...
44
          private static ReturnInfoJSON _GetReturninfoJSON(string url, int timeoutinseconds, out string returninfojsonstring)
5df5f3e5   Schwirg László   v1.14.0.0
45
          {
c4219940   Schwirg László   WebApplicationMan...
46
47
48
49
50
51
52
              Stream returninfojsonstream;
              using (var httpclient = new HttpClient())
              {
                  httpclient.Timeout = new TimeSpan(0, 0, timeoutinseconds);
                  returninfojsonstream = Task.Run(async () => await httpclient.GetStreamAsync(url)).GetAwaiter().GetResult();
              }
  
5df5f3e5   Schwirg László   v1.14.0.0
53
              returninfojsonstring = GetStreamAsString(returninfojsonstream);
aa2e3807   Schwirg László   v1.15.0
54
              ReturnInfoJSON returninfojson = (ReturnInfoJSON)JsonConvert.DeserializeObject(returninfojsonstring,typeof(ReturnInfoJSON));
5df5f3e5   Schwirg László   v1.14.0.0
55
56
57
              //ReturnInfoJSON returninfojson = Task.Run(async () => await JsonSerializer.DeserializeAsync<ReturnInfoJSON>(returninfojsonstream)).GetAwaiter().GetResult();
              return returninfojson;
          }
c4219940   Schwirg László   WebApplicationMan...
58
          private static ReturnInfoJSON _PostReturninfoJSON(string url, int timeoutinseconds, out string returninfojsonstring, List<Vrh.XmlProcessing.UrlElement.UrlParameter> dictparameterlist = null)
5df5f3e5   Schwirg László   v1.14.0.0
59
          {
aa2e3807   Schwirg László   v1.15.0
60
61
              var jsonstring = JsonConvert.SerializeObject(dictparameterlist.Where(p => p.PassTo == Vrh.XmlProcessing.UrlElement.ParameterTypes.dict));
              HttpContent requestcontent = new StringContent(jsonstring);
c4219940   Schwirg László   WebApplicationMan...
62
63
64
65
66
67
68
69
  
              HttpResponseMessage returninfojsonhttpresponsemessage;
              using (var httpclient = new HttpClient())
              {
                  httpclient.Timeout = new TimeSpan(0, 0, timeoutinseconds);
                  returninfojsonhttpresponsemessage = Task.Run(async () => await httpclient.PostAsync(url, requestcontent)).GetAwaiter().GetResult();
              }
  
5df5f3e5   Schwirg László   v1.14.0.0
70
71
              var returninfojsonstream = Task.Run(async () => await returninfojsonhttpresponsemessage.Content.ReadAsStreamAsync()).GetAwaiter().GetResult();
              returninfojsonstring = GetStreamAsString(returninfojsonstream);
aa2e3807   Schwirg László   v1.15.0
72
              ReturnInfoJSON returninfojson = (ReturnInfoJSON)JsonConvert.DeserializeObject(returninfojsonstring, typeof(ReturnInfoJSON));
5df5f3e5   Schwirg László   v1.14.0.0
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
              //var returninfojson = Task.Run(async () => await JsonSerializer.DeserializeAsync<ReturnInfoJSON>(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
          {
5df5f3e5   Schwirg László   v1.14.0.0
89
90
              public int ReturnValue { get; set; }
  
5df5f3e5   Schwirg László   v1.14.0.0
91
92
93
94
              public string ReturnMessage { get; set; }
          }
      }
  }