-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
72 lines (63 loc) · 2.34 KB
/
Program.cs
File metadata and controls
72 lines (63 loc) · 2.34 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
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
namespace MakeWebRequest
{
class Program
{
/// <summary>
/// Sends a request to the specified URL and gives back the response content
/// </summary>
/// <param name="url">Endpoint to call (Required)</param>
/// <param name="requestVerb">Http Request Method (Required)</param>
/// <param name="requestBody">Body to send with Request</param>
/// <param name="outFile">Location to save request body</param>
public static async Task<int> Main(string url, string requestVerb, string requestBody, string outFile)
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddHttpClient<InternalHttpClient>();
var serviceProvider = serviceCollection.BuildServiceProvider();
Uri requestUri;
HttpMethod requestMethod;
try
{
requestUri = new Uri(url);
}
catch
{
Console.WriteLine("Unable to parse URL! Please check and try again");
return 1;
}
try
{
requestMethod = HttpMethodResolver.Resolve(requestVerb);
}
catch
{
return 2;
}
var request = RequestBuilder.BuildRequestMessage(requestUri, requestMethod, requestBody);
var response = await serviceProvider.GetRequiredService<InternalHttpClient>().SendRequestAsync(request);
if (!string.IsNullOrWhiteSpace(outFile))
{
Console.WriteLine($"Attempting to save to: {outFile}");
try
{
File.WriteAllText(outFile, response);
}
catch
{
Console.WriteLine("Error writing response to file");
Console.WriteLine("Response:");
Console.WriteLine(response);
return 3;
}
}
Console.WriteLine("Response:");
Console.WriteLine(response);
return 0;
}
}
}