-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebClient.lua
68 lines (54 loc) · 2 KB
/
WebClient.lua
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
WebClient = {};
luanet.load_assembly("log4net")
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");
-- Create a logger
local log = nil;
if(rootLogger) then
log = types["log4net.LogManager"].GetLogger(rootLogger .. ".WebClient");
else
log = types["log4net.LogManager"].GetLogger("WebClient");
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:DebugFormat("Unable to get response from the request url: {0}", error);
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}", error);
return nil;
end
else
log:Info("Unable to read response content");
end
return nil;
end
--Exports
WebClient.GetRequest = GetRequest;
WebClient.ReadResponse = ReadResponse;