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,}
|
aa2e3807
Schwirg László
v1.15.0
|
33
|
public static ReturnInfoJSON GetReturninfoJSON(string url, RequestType mode = RequestType.GET, List<Vrh.XmlProcessing.UrlElement.UrlParameter> dictparameterlist=null)
|
5df5f3e5
Schwirg László
v1.14.0.0
|
34
35
36
37
|
{
string returninfojsonstring = "";
try
{
|
aa2e3807
Schwirg László
v1.15.0
|
38
|
return mode == RequestType.GET? _GetReturninfoJSON(url,out returninfojsonstring) : _PostReturninfoJSON(url, out returninfojsonstring, dictparameterlist);
|
5df5f3e5
Schwirg László
v1.14.0.0
|
39
40
41
42
43
44
45
46
|
}
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);
|
aa2e3807
Schwirg László
v1.15.0
|
47
|
ReturnInfoJSON returninfojson = (ReturnInfoJSON)JsonConvert.DeserializeObject(returninfojsonstring,typeof(ReturnInfoJSON));
|
5df5f3e5
Schwirg László
v1.14.0.0
|
48
49
50
|
//ReturnInfoJSON returninfojson = Task.Run(async () => await JsonSerializer.DeserializeAsync<ReturnInfoJSON>(returninfojsonstream)).GetAwaiter().GetResult();
return returninfojson;
}
|
aa2e3807
Schwirg László
v1.15.0
|
51
|
private static ReturnInfoJSON _PostReturninfoJSON(string url, out string returninfojsonstring, List<Vrh.XmlProcessing.UrlElement.UrlParameter> dictparameterlist = null)
|
5df5f3e5
Schwirg László
v1.14.0.0
|
52
|
{
|
aa2e3807
Schwirg László
v1.15.0
|
53
54
55
|
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();
|
5df5f3e5
Schwirg László
v1.14.0.0
|
56
57
|
var returninfojsonstream = Task.Run(async () => await returninfojsonhttpresponsemessage.Content.ReadAsStreamAsync()).GetAwaiter().GetResult();
returninfojsonstring = GetStreamAsString(returninfojsonstream);
|
aa2e3807
Schwirg László
v1.15.0
|
58
|
ReturnInfoJSON returninfojson = (ReturnInfoJSON)JsonConvert.DeserializeObject(returninfojsonstring, typeof(ReturnInfoJSON));
|
5df5f3e5
Schwirg László
v1.14.0.0
|
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
//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
|
75
76
|
public int ReturnValue { get; set; }
|
5df5f3e5
Schwirg László
v1.14.0.0
|
77
78
79
80
|
public string ReturnMessage { get; set; }
}
}
}
|