-
Notifications
You must be signed in to change notification settings - Fork 13
/
Downloader.cs
49 lines (42 loc) · 1.03 KB
/
Downloader.cs
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
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
namespace Emoji
{
class Downloader
{
private readonly HttpClient _client = new HttpClient();
public async Task<string> DownloadEmojiListAsync()
{
var request = new HttpRequestMessage
{
RequestUri = new Uri("https://api.github.com/emojis"),
Headers =
{
{ "Connection", "Keep-Alive"},
{ "Accept", "application/vnd.github.v3+json"},
{ "User-Agent", "EmojiVS" },
},
};
var response = await _client.SendAsync(request);
return await response.Content.ReadAsStringAsync();
}
public async Task DownloadEmojiAsync(Emoji emoji)
{
var request = new HttpRequestMessage
{
RequestUri = emoji.Uri,
Headers =
{
{ "Connection", "Keep-Alive"},
{ "User-Agent", "EmojiVS" },
},
};
var response = await _client.SendAsync(request);
var stream = await response.Content.ReadAsStreamAsync();
using (var file = File.OpenWrite(emoji.FileName))
await stream.CopyToAsync(file);
}
}
}