-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.cs
More file actions
49 lines (42 loc) · 1.32 KB
/
program.cs
File metadata and controls
49 lines (42 loc) · 1.32 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
void Main()
{
// Nuget JSON.Net
// using System.Net.Http
const string GetUrl = "http://84.200.109.239:5000/challenges/1/";
const string PostUrl = "http://84.200.109.239:5000/solutions/1/";
using (var clientGet = CreateClient())
{
var responseGet = clientGet.GetAsync(GetUrl).Result;
var resultGet = responseGet.Content.ReadAsStringAsync().Result;
resultGet.Dump("Get Response"); // Linqpad
if (string.IsNullOrEmpty(resultGet) == false)
{
using (var client = CreateClient())
{
var solResult = new SolutionResult() { Token = resultGet };
var responsePost = client.PostAsync(PostUrl, solResult.AsJson()).Result;
var resultPost = responsePost.Content.ReadAsStringAsync().Result;
resultPost.Dump("Post Response"); // Linqpad
}
}
}
}
private class SolutionResult
{
[JsonProperty("token")]
public string Token { get; set; }
}
private static HttpClient CreateClient(string accessToken = "")
{
var client = new HttpClient();
if (!string.IsNullOrWhiteSpace(accessToken))
{
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
}
return client;
}
public static class Extensions
{
public static StringContent AsJson(this object o)
=> new StringContent(JsonConvert.SerializeObject(o), Encoding.UTF8, "application/json");
}