Blame view

Vrh.Log4Pro.MaintenanceConsole/ConsoleFunction - Tools - Http.cs 3.41 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  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 System.Text.Json;
  using System.Text.Json.Serialization;
  
  
  namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS
  {
      public static class HttpTools
      {
          public enum RequestType { GET,POST,}
          public static ReturnInfoJSON GetReturninfoJSON(string url, RequestType mode = RequestType.GET)
          {
              string returninfojsonstring = "";
              try
              {
                  return mode == RequestType.GET? _GetReturninfoJSON(url,out returninfojsonstring) : _PostReturninfoJSON(url, out returninfojsonstring);
              }
              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 = JsonSerializer.Deserialize<ReturnInfoJSON>(returninfojsonstring);
              //ReturnInfoJSON returninfojson = Task.Run(async () => await JsonSerializer.DeserializeAsync<ReturnInfoJSON>(returninfojsonstream)).GetAwaiter().GetResult();
              return returninfojson;
          }
          private static ReturnInfoJSON _PostReturninfoJSON(string url, out string returninfojsonstring)
          {
              var returninfojsonhttpresponsemessage = Task.Run(async () => await (new HttpClient()).PostAsync(url, null)).GetAwaiter().GetResult();
              var returninfojsonstream = Task.Run(async () => await returninfojsonhttpresponsemessage.Content.ReadAsStreamAsync()).GetAwaiter().GetResult();
              returninfojsonstring = GetStreamAsString(returninfojsonstream);
              ReturnInfoJSON returninfojson = JsonSerializer.Deserialize<ReturnInfoJSON>(returninfojsonstring);
              //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
          {
              [JsonPropertyName("ReturnValue")]
              public int ReturnValue { get; set; }
  
              [JsonPropertyName("ReturnMessage")]
              public string ReturnMessage { get; set; }
          }
      }
  }