回答編集履歴
3
誤字の修正
test
CHANGED
@@ -182,7 +182,7 @@
|
|
182
182
|
|
183
183
|
|
184
184
|
|
185
|
-
//
|
185
|
+
// HttpClientのヘッダにセットする
|
186
186
|
|
187
187
|
httpClient.DefaultRequestHeaders.Add("key", API_KEY);
|
188
188
|
|
2
HttpClientを使った方法を追加
test
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
WebClientを使った方法
|
2
|
+
|
1
3
|
```
|
2
4
|
|
3
5
|
// 参照に以下の2つを追加する。
|
@@ -104,16 +106,140 @@
|
|
104
106
|
|
105
107
|
}
|
106
108
|
|
107
|
-
|
108
|
-
|
109
|
-
```
|
109
|
+
```
|
110
|
+
|
111
|
+
|
112
|
+
|
110
|
-
|
113
|
+
HttpClientを使った方法
|
111
|
-
|
112
|
-
|
114
|
+
|
113
|
-
```
|
115
|
+
```
|
116
|
+
|
117
|
+
using Newtonsoft.Json;
|
118
|
+
|
119
|
+
using System;
|
120
|
+
|
121
|
+
using System.Collections.Generic;
|
122
|
+
|
123
|
+
using System.Net.Http;
|
124
|
+
|
125
|
+
using System.Security.Cryptography;
|
126
|
+
|
127
|
+
using System.Text;
|
128
|
+
|
129
|
+
using System.Threading.Tasks;
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
namespace zaif_test_httpclient
|
114
134
|
|
115
135
|
{
|
116
136
|
|
137
|
+
class Program
|
138
|
+
|
139
|
+
{
|
140
|
+
|
141
|
+
static readonly string API_TRADE_ENDPOINT = "https://api.zaif.jp/tapi";
|
142
|
+
|
143
|
+
static readonly string API_KEY = "***";
|
144
|
+
|
145
|
+
static readonly string API_SECRET = "***";
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
static async Task<object> MyTask()
|
150
|
+
|
151
|
+
{
|
152
|
+
|
153
|
+
var httpClient = new HttpClient();
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
// nonce用にunixtimeを用いる
|
158
|
+
|
159
|
+
var unixtime = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
// POSTするデータ
|
164
|
+
|
165
|
+
var content = new FormUrlEncodedContent(new Dictionary<string, string>
|
166
|
+
|
167
|
+
{
|
168
|
+
|
169
|
+
{ "nonce", unixtime.ToString() },
|
170
|
+
|
171
|
+
{ "method", "get_info" }
|
172
|
+
|
173
|
+
});
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
// Sign を計算する
|
178
|
+
|
179
|
+
var hash = new HMACSHA512(new UTF8Encoding().GetBytes(API_SECRET)).ComputeHash(new UTF8Encoding().GetBytes(await content.ReadAsStringAsync()));
|
180
|
+
|
181
|
+
var sign = BitConverter.ToString(hash).ToLower().Replace("-", "");
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
// WebClientのヘッダにセットする
|
186
|
+
|
187
|
+
httpClient.DefaultRequestHeaders.Add("key", API_KEY);
|
188
|
+
|
189
|
+
httpClient.DefaultRequestHeaders.Add("Sign", sign);
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
// POST実行
|
194
|
+
|
195
|
+
var res = await httpClient.PostAsync(API_TRADE_ENDPOINT, content);
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
// 結果JSONのデシリアライズ
|
200
|
+
|
201
|
+
var json = JsonConvert.DeserializeObject(await res.Content.ReadAsStringAsync());
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
// 結果を返す
|
206
|
+
|
207
|
+
return json;
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
static void Main()
|
214
|
+
|
215
|
+
{
|
216
|
+
|
217
|
+
Task.Run(async () =>
|
218
|
+
|
219
|
+
{
|
220
|
+
|
221
|
+
var obj = await MyTask();
|
222
|
+
|
223
|
+
Console.WriteLine(obj);
|
224
|
+
|
225
|
+
}).Wait();
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
```
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
```
|
240
|
+
|
241
|
+
{
|
242
|
+
|
117
243
|
"success": 1,
|
118
244
|
|
119
245
|
"return": {
|
1
参照の情報を追加
test
CHANGED
@@ -1,4 +1,16 @@
|
|
1
1
|
```
|
2
|
+
|
3
|
+
// 参照に以下の2つを追加する。
|
4
|
+
|
5
|
+
// System.Security
|
6
|
+
|
7
|
+
// System.Web
|
8
|
+
|
9
|
+
//
|
10
|
+
|
11
|
+
// NuGetから以下1つのパッケージをインストールする。
|
12
|
+
|
13
|
+
// Newtonsoft.Json
|
2
14
|
|
3
15
|
using Newtonsoft.Json;
|
4
16
|
|