回答編集履歴
1
見直しキャンペーン中
test
CHANGED
@@ -1,147 +1,74 @@
|
|
1
1
|
`GET`で`HttpClient`を作り直してるからじゃないですか?
|
2
|
-
|
3
2
|
あとクッキーを使いたいだけなら、`UseCookies = true`でいいんじゃないですかね。
|
4
3
|
|
5
|
-
|
6
|
-
|
7
|
-
```
|
4
|
+
```cs
|
8
|
-
|
9
5
|
using System;
|
10
|
-
|
11
6
|
using System.Diagnostics;
|
12
|
-
|
13
7
|
using System.Linq;
|
14
|
-
|
15
8
|
using System.Net.Http;
|
16
|
-
|
17
9
|
using System.Text;
|
18
|
-
|
19
10
|
using System.Threading.Tasks;
|
20
11
|
|
21
|
-
|
22
|
-
|
23
12
|
namespace Questions251616
|
24
|
-
|
25
13
|
{
|
26
|
-
|
27
14
|
static class Program
|
28
|
-
|
29
15
|
{
|
30
|
-
|
31
16
|
static async Task Main()
|
32
|
-
|
33
17
|
{
|
34
|
-
|
35
18
|
Console.Write("username >>>");
|
36
|
-
|
37
19
|
var user = Console.ReadLine();
|
38
20
|
|
39
|
-
|
40
|
-
|
41
21
|
Console.Write("password >>>");
|
42
|
-
|
43
22
|
var password = Console.ReadLine();
|
44
23
|
|
45
|
-
|
46
|
-
|
47
24
|
var obj = new API();
|
48
|
-
|
49
25
|
var result = await obj.LoginAsync(user, password);
|
50
|
-
|
51
26
|
Console.WriteLine(result);
|
52
|
-
|
53
27
|
Console.WriteLine();
|
54
28
|
|
55
|
-
|
56
|
-
|
57
29
|
var self = await obj.SelfAsync();
|
58
|
-
|
59
30
|
Console.WriteLine(self);
|
60
31
|
|
61
|
-
|
62
|
-
|
63
32
|
Console.ReadKey();
|
64
|
-
|
65
33
|
}
|
66
|
-
|
67
34
|
}
|
68
35
|
|
69
|
-
|
70
|
-
|
71
36
|
public class API
|
72
|
-
|
73
37
|
{
|
74
|
-
|
75
38
|
// 長いw
|
76
|
-
|
77
39
|
private static readonly HttpClient client = new HttpClient(new HttpClientHandler() { UseCookies = true, })
|
78
|
-
|
79
40
|
{
|
80
|
-
|
81
41
|
BaseAddress = new Uri("https://judgeapi.u-aizu.ac.jp/"),
|
82
|
-
|
83
42
|
};
|
84
43
|
|
85
|
-
|
86
|
-
|
87
44
|
public async Task<string> LoginAsync(string id, string pass)
|
88
|
-
|
89
45
|
{
|
90
|
-
|
91
46
|
var json = $@"{{""id"":""{id}"",""password"":""{pass}""}}";
|
92
|
-
|
93
47
|
Debug.WriteLine(json);
|
94
48
|
|
95
|
-
|
96
|
-
|
97
49
|
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
98
|
-
|
99
50
|
var response = await client.PostAsync("session", content);
|
100
|
-
|
101
51
|
Debug.WriteLine($"content ->> {content}");
|
102
52
|
|
103
|
-
|
104
|
-
|
105
53
|
if(response.Headers.Contains("Set-Cookie"))
|
106
|
-
|
107
54
|
{
|
108
|
-
|
109
55
|
var cookie = response.Headers.GetValues("Set-Cookie").First();
|
110
|
-
|
111
56
|
Debug.WriteLine($"cookie --> {cookie}\n");
|
112
|
-
|
113
57
|
}
|
114
58
|
|
115
|
-
|
116
|
-
|
117
59
|
return await response.Content.ReadAsStringAsync();
|
118
|
-
|
119
60
|
}
|
120
|
-
|
121
|
-
|
122
61
|
|
123
62
|
public Task<string> SelfAsync() => GetAsync("self");
|
124
63
|
|
125
|
-
|
126
|
-
|
127
64
|
public async Task<string> GetAsync(string uri)
|
128
|
-
|
129
65
|
{
|
130
|
-
|
131
66
|
var response = await client.GetAsync(uri);
|
132
|
-
|
133
67
|
return await response.Content.ReadAsStringAsync();
|
134
|
-
|
135
68
|
}
|
136
|
-
|
137
69
|
}
|
138
|
-
|
139
70
|
}
|
140
|
-
|
141
71
|
```
|
142
72
|
|
143
|
-
|
144
|
-
|
145
73
|
[.NET(Framework)のHttpClientの取り扱いには要注意という話 - Qiita](https://qiita.com/nskhara/items/b7c31d60531ffbe29537)
|
146
|
-
|
147
74
|
を見ると`Static`も`UseCookies = true`も要注意なのですが^^;
|