-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebClient.lua
More file actions
107 lines (89 loc) · 3.67 KB
/
WebClient.lua
File metadata and controls
107 lines (89 loc) · 3.67 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
WebClient = {};
local types = {};
types["log4net.LogManager"] = luanet.import_type("log4net.LogManager");
types["System.Net.WebClient"] = luanet.import_type("System.Net.WebClient");
types["System.Text.Encoding"] = luanet.import_type("System.Text.Encoding");
types["System.Xml.XmlTextReader"] = luanet.import_type("System.Xml.XmlTextReader");
types["System.Xml.XmlDocument"] = luanet.import_type("System.Xml.XmlDocument");
types["System.IO.StreamReader"] = luanet.import_type("System.IO.StreamReader");
-- Create a logger
local log = types["log4net.LogManager"].GetLogger(rootLogger .. ".WebClient");
-- Helper to safely unpack nested web exception messages and response bodies
local function GetWebExceptionMessage(exception)
local message = "";
if exception and exception.Message then
message = exception.Message;
if (exception.InnerException) then
message = message .. "\r\n" .. GetWebExceptionMessage(exception.InnerException);
if exception.InnerException.Response and exception.InnerException.Response ~= "Response" then
-- This is necessary to get the response body from exceptions thrown by WebClients.
local streamReader = types["System.IO.StreamReader"](exception.InnerException.Response:GetResponseStream());
local responseContent = streamReader:ReadToEnd();
log:DebugFormat("Web exception response: {0}", Utility.Redact(responseContent));
end
end
elseif exception then
message = tostring(exception);
end
return message;
end
local function GetRequest(requestUrl, headers)
local webClient = types["System.Net.WebClient"]();
local response = nil;
log:Debug("Created Web Client");
webClient.Encoding = types["System.Text.Encoding"].UTF8;
for _, header in ipairs(headers) do
webClient.Headers:Add(header);
end
local success, error = pcall(function ()
response = webClient:DownloadString(requestUrl);
end);
webClient:Dispose();
log:Debug("Disposed Web Client");
if(success) then
return response;
else
log:InfoFormat("Unable to get response from the request url: {0}", Utility.Redact(GetWebExceptionMessage(error)));
end
end
-- Handle POST requests
local function PostRequest(requestUrl, headers, body)
local webClient = types["System.Net.WebClient"]();
local response = nil;
log:Debug("Created Web Client for POST");
webClient.Encoding = types["System.Text.Encoding"].UTF8;
for _, header in ipairs(headers) do
webClient.Headers:Add(header);
end
local success, error = pcall(function ()
response = webClient:UploadString(requestUrl, "POST", body);
end);
webClient:Dispose();
log:Debug("Disposed Web Client");
if(success) then
return response;
else
log:InfoFormat("Unable to post response to the request url: {0}", Utility.Redact(GetWebExceptionMessage(error)));
return nil;
end
end
local function ReadResponse( responseString )
if (responseString and #responseString > 0) then
local responseDocument = types["System.Xml.XmlDocument"]();
local documentLoaded, error = pcall(function ()
responseDocument:LoadXml(responseString);
end);
if (documentLoaded) then
return responseDocument;
else
log:InfoFormat("Unable to load response content as XML: {0}", Utility.Redact(tostring(error)));
return nil;
end
else
log:Info("Response string is nil or empty.");
return nil;
end
end
WebClient.GetRequest = GetRequest;
WebClient.PostRequest = PostRequest;
WebClient.ReadResponse = ReadResponse;