-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathListing05.33.CustomParameterValidation.cs
More file actions
60 lines (56 loc) · 1.7 KB
/
Listing05.33.CustomParameterValidation.cs
File metadata and controls
60 lines (56 loc) · 1.7 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
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_33;
#region INCLUDE
public class Program
{
#region HIGHLIGHT
public static int Main(string[] args)
#endregion HIGHLIGHT
{
#region HIGHLIGHT
int result = 0;
#endregion HIGHLIGHT
if(args.Length != 2 )
{
// Exactly two arguments must be specified; give an error
Console.WriteLine(
"ERROR: You must specify the "
+ "URL and the file name");
Console.WriteLine(
"Usage: Downloader.exe <URL> <TargetFileName>");
result = 1;
}
else
{
DownloadSSL(args[0], args[1]);
}
return result;
}
private static void DownloadSSL(string httpsUrl, string fileName)
{
#if !NET7_0_OR_GREATER
httpsUrl = httpsUrl?.Trim() ??
throw new ArgumentNullException(nameof(httpsUrl));
fileName = fileName ??
throw new ArgumentNullException(nameof(fileName));
if (fileName.Trim().Length == 0)
{
throw new ArgumentException(
$"{nameof(fileName)} cannot be empty or only whitespace");
}
#else
ArgumentException.ThrowIfNullOrEmpty(httpsUrl = httpsUrl?.Trim()!);
ArgumentException.ThrowIfNullOrEmpty(fileName = fileName?.Trim()!);
#endif
if (!httpsUrl.ToUpper().StartsWith("HTTPS"))
{
throw new ArgumentException("URL must start with 'HTTPS'.");
}
HttpClient client = new();
byte[] response =
client.GetByteArrayAsync(httpsUrl).Result;
client.Dispose();
File.WriteAllBytes(fileName!, response);
Console.WriteLine($"Downloaded '{fileName}' from '{httpsUrl}'.");
}
}
#endregion INCLUDE