Skip to content

Commit 01526d7

Browse files
committed
v1.2.0
- Upgrade to .NET 8.0 - Migrate from WebClient to HttpClient
1 parent e7628bc commit 01526d7

File tree

4 files changed

+106
-71
lines changed

4 files changed

+106
-71
lines changed

CLARiNET.csproj

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
5+
<TargetFramework>net8.0</TargetFramework>
66
<Authors>Whitley Media</Authors>
77
<PackageIcon>CLARiNET.ico</PackageIcon>
88
<PackageIconUrl />
99
<ApplicationIcon>CLARiNET.ico</ApplicationIcon>
10-
<Version>1.1.2</Version>
10+
<Version>1.2.0</Version>
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="CommandLineParser" Version="2.8.0" />
15-
<PackageReference Include="HtmlAgilityPack" Version="1.11.39" />
16-
<PackageReference Include="Microsoft.AspNetCore.DataProtection" Version="6.0.1" />
17-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
18-
<PackageReference Include="System.Security.Cryptography.ProtectedData" Version="6.0.0" />
14+
<PackageReference Include="CommandLineParser" Version="2.9.1" />
15+
<PackageReference Include="HtmlAgilityPack" Version="1.11.55" />
16+
<PackageReference Include="Microsoft.AspNetCore.DataProtection" Version="8.0.0" />
17+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
18+
<PackageReference Include="System.Security.Cryptography.ProtectedData" Version="8.0.0" />
1919
</ItemGroup>
2020

2121
<ItemGroup>

Extensions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Net.Http.Headers;
5+
using System.Net.Http;
46
using System.Text;
57
using System.Threading.Tasks;
68

@@ -29,5 +31,13 @@ public static IEnumerable<IEnumerable<TSource>> Batch<TSource>(
2931
if (count > 0)
3032
yield return items.Take(count);
3133
}
34+
35+
36+
public static void BasicAuth(this HttpRequestMessage http, string username, string password)
37+
{
38+
var authenticationString = $"{username}:{password}";
39+
var base64String = Convert.ToBase64String(Encoding.ASCII.GetBytes(authenticationString));
40+
http.Headers.Authorization = new AuthenticationHeaderValue("Basic", base64String);
41+
}
3242
}
3343
}

Photo.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public static string Download(Options options, string file, string soapUrl)
1818
string workerRef = Resources.Worker_Reference;
1919
string result = "";
2020
string path = Path.GetDirectoryName(file);
21+
int batchErrors = 0;
22+
int errors = 0;
23+
int batchNum = 1;
2124

2225
Dictionary<string,string> ids = File.ReadLines(file).Select(line => line.Split(',')).ToDictionary(line => line[0], line => line.Count() > 1 ? line[1] : "");
2326

@@ -72,19 +75,28 @@ public static string Download(Options options, string file, string soapUrl)
7275
{
7376
Console.WriteLine("\n\nError: " + ex3.Message);
7477
Console.WriteLine("\n");
78+
errors++;
7579
}
7680
}
7781
}
7882
}
83+
else
84+
{
85+
Console.WriteLine("\n\nBatch {0:N0} Error: No data returned.\n\n{1}", batchNum, result);
86+
Console.WriteLine("\n");
87+
errors++;
88+
}
7989

8090
}
8191
catch(Exception batchEx)
8292
{
83-
Console.WriteLine("\n\nError: " + batchEx.Message);
93+
Console.WriteLine("\n\nBatch {0:N0} Error: {1}", batchNum, batchEx.Message);
8494
Console.WriteLine("\n");
95+
batchErrors++;
8596
}
97+
batchNum++;
8698
}
87-
result = String.Format("Processed {0:N0} id{1}.", ids.Count(), ids.Count() == 1 ? "" : "s");
99+
result = String.Format("Processed {0:N0} id{1}\nFile Errors: {2:N0}\nBatch Errors: {3:N0}\n", ids.Count(), ids.Count() == 1 ? "" : "s", errors, batchErrors);
88100
}
89101
catch (Exception ex)
90102
{

WDWebService.cs

Lines changed: 75 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,37 @@
77
using System.Text;
88
using System.IO;
99
using HtmlAgilityPack;
10+
using System.Net.Http;
11+
using System.Reflection.Metadata;
12+
using System.Net.Http.Headers;
13+
using static System.Net.WebRequestMethods;
14+
using static System.Runtime.InteropServices.JavaScript.JSType;
15+
using System.Reflection.PortableExecutable;
16+
using System.Xml.XPath;
1017

1118
namespace CLARiNET
1219
{
1320
public class WDWebService
1421
{
22+
static readonly HttpClient _client = new HttpClient();
23+
1524
public static byte[] CallRest(string tenant, string username, string password, string url, string method, byte[] data)
1625
{
17-
using (var webClient = new WebClient())
18-
{
19-
ServicePointManager.Expect100Continue = true;
20-
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
21-
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
22-
if (!String.IsNullOrEmpty(username))
23-
{
24-
webClient.Credentials = new NetworkCredential(username, password);
25-
webClient.Headers.Add("X-Originator", "CLARiNET");
26-
webClient.Headers.Add("X-Tenant", tenant);
27-
if (method == WebRequestMethods.Http.Get)
28-
{
29-
return webClient.DownloadData(url);
30-
}
31-
else
32-
{
33-
return webClient.UploadData(url, data);
34-
}
35-
}
26+
HttpRequestMessage http = new HttpRequestMessage();
27+
http.Headers.Add("X-Originator", "CLARiNET");
28+
http.Headers.Add("X-Tenant", tenant);
29+
http.RequestUri = new Uri(url);
30+
http.Method = new HttpMethod(method);
31+
http.BasicAuth(username, password);
32+
33+
if (method != WebRequestMethods.Http.Get)
34+
{
35+
http.Content = new ByteArrayContent(data);
3636
}
37-
return null;
37+
38+
HttpResponseMessage response = _client.Send(http);
39+
return response.Content.ReadAsByteArrayAsync().Result;
40+
3841
}
3942

4043
public static string WrapSOAP(string username, string password, string xmlBody)
@@ -120,10 +123,13 @@ public static Dictionary<string, string> Download(string url)
120123
string html = "";
121124
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
122125

123-
using (var webClient = new WebClient())
124-
{
125-
html = webClient.DownloadString(url);
126-
}
126+
HttpRequestMessage http = new HttpRequestMessage();
127+
http.RequestUri = new Uri(url);
128+
129+
130+
HttpResponseMessage response = _client.Send(http);
131+
html = response.Content.ReadAsStringAsync().Result;
132+
127133
htmlDoc.LoadHtml(html);
128134

129135
HtmlNodeCollection nodes = htmlDoc.DocumentNode.SelectNodes("//a[contains(@href, '.xsd')]");
@@ -144,60 +150,67 @@ public static Dictionary<string, string> Load(string data)
144150

145151
public static string GetServiceURL(string envURL, string tenant, string username, string password)
146152
{
147-
string result = "";
148153

149-
using (var webClient = new WebClient())
150-
{
151-
ServicePointManager.Expect100Continue = true;
152-
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
153-
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
154-
webClient.Credentials = new NetworkCredential(username + "@" + tenant, password);
155-
result = webClient.DownloadString(envURL + "/cc-cloud-master/service-gateway");
156-
}
154+
HttpRequestMessage http = new HttpRequestMessage();
155+
http.RequestUri = new Uri(envURL + "/cc-cloud-master/service-gateway");
156+
http.BasicAuth(username + "@" + tenant, password);
157+
158+
HttpResponseMessage response = _client.Send(http);
159+
return response.Content.ReadAsStringAsync().Result;
157160

158-
return result;
159161
}
160162

161163
public static string CallAPI(string username, string password, string url, string xmlData)
162164
{
163165
try
164166
{
165-
using (var webClient = new WebClient())
166-
{
167-
webClient.Headers.Add("Content-Type", "text/xml; charset=utf-8");
168-
ServicePointManager.Expect100Continue = true;
169-
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
170-
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
171-
webClient.Credentials = new NetworkCredential(username, password);
172-
byte[] data = Encoding.UTF8.GetBytes(WDWebService.WrapSOAP(username, password, xmlData));
173-
byte[] rData = webClient.UploadData(url, data);
174-
175-
return new XDeclaration("1.0", "UTF-8", null).ToString() + Environment.NewLine + XDocument.Parse(Encoding.UTF8.GetString(rData)).ToString() + Environment.NewLine;
176-
}
177-
}
178-
catch (WebException webEx)
179-
{
180-
String responseFromServer = webEx.Message.ToString() + Environment.NewLine;
181-
if (webEx.Response != null)
167+
HttpRequestMessage http = new HttpRequestMessage();
168+
169+
http.RequestUri = new Uri(url);
170+
http.Method = new HttpMethod(WebRequestMethods.Http.Post);
171+
http.BasicAuth(username, password);
172+
173+
174+
byte[] data = Encoding.UTF8.GetBytes(WDWebService.WrapSOAP(username, password, xmlData));
175+
http.Content = new ByteArrayContent(data);
176+
http.Content.Headers.Add("Content-Type", "text/xml; charset=utf-8");
177+
178+
HttpResponseMessage response = _client.Send(http);
179+
180+
if (!response.IsSuccessStatusCode)
182181
{
183-
using (WebResponse response = webEx.Response)
182+
try
184183
{
185-
Stream dataRs = response.GetResponseStream();
186-
using (StreamReader reader = new StreamReader(dataRs))
184+
string result = response.Content.ReadAsStringAsync().Result;
185+
var xDoc = XDocument.Parse(result);
186+
XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable());
187+
ns.AddNamespace("wd", "urn:com.workday/bsvc");
188+
if (xDoc != null)
187189
{
188-
try
189-
{
190-
responseFromServer += XDocument.Parse(reader.ReadToEnd());
191-
}
192-
catch
193-
{
194-
// ignore exception
195-
}
190+
result = xDoc.XPathSelectElement("//faultstring", ns).Value;
196191
}
192+
return result;
197193
}
194+
catch
195+
{
196+
// ignore exception
197+
}
198+
return null;
199+
198200
}
201+
202+
byte[] rData = response.Content.ReadAsByteArrayAsync().Result;
203+
return new XDeclaration("1.0", "UTF-8", null).ToString() + Environment.NewLine + XDocument.Parse(Encoding.UTF8.GetString(rData)).ToString() + Environment.NewLine;
204+
205+
}
206+
catch (HttpRequestException webEx)
207+
{
208+
209+
string responseFromServer = webEx.Message.ToString() + Environment.NewLine;
199210
return responseFromServer;
200211
}
201212
}
213+
214+
202215
}
203216
}

0 commit comments

Comments
 (0)