-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDownloadClient.cs
More file actions
101 lines (83 loc) · 3.26 KB
/
DownloadClient.cs
File metadata and controls
101 lines (83 loc) · 3.26 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using ModApi.Common;
using ModAPI.Common.Update;
using System;
using System.IO;
using System.IO.Pipes;
using System.Net.Http;
namespace ModAPI.Common
{
public class DownloadClient : IDisposable
{
private HttpClient httpClient = new HttpClient();
private HttpRequestMessage httpRequestMessage = new HttpRequestMessage();
private static readonly string httpUserAgent = "Spore-ModAPI-Launcher-Kit/" + UpdateManager.CurrentVersion.ToString();
public event StreamUtils.StreamProgressEventHandler DownloadProgressChanged = null;
public DownloadClient(string url)
{
httpClient.Timeout = TimeSpan.FromMinutes(5);
httpRequestMessage.RequestUri = new Uri(url);
httpRequestMessage.Headers.Add("User-Agent", httpUserAgent);
}
public void SetTimeout(TimeSpan timeout)
{
httpClient.Timeout = timeout;
}
public void AddHeader(string key, string value)
{
httpRequestMessage.Headers.Add(key, value);
}
public string DownloadToString()
{
var response = httpClient.SendAsync(httpRequestMessage).Result;
if (!response.IsSuccessStatusCode)
{
throw new HttpRequestException($"Received unsuccessful status code: {(int)response.StatusCode} {response.StatusCode}");
}
return response.Content.ReadAsStringAsync().Result;
}
public void DownloadToFile(string file)
{
var response = httpClient.SendAsync(httpRequestMessage).Result;
if (!response.IsSuccessStatusCode)
{
throw new HttpRequestException($"Received unsuccessful status code: {(int)response.StatusCode} {response.StatusCode}");
}
using (var downloadStream = response.Content.ReadAsStreamAsync().Result)
using (var fileStream = new FileStream(file, FileMode.Create))
{
StreamUtils.CopyStreamWithProgress(downloadStream, fileStream, this, DownloadProgressChanged);
}
}
public MemoryStream DownloadToMemory()
{
var response = httpClient.SendAsync(httpRequestMessage).Result;
if (!response.IsSuccessStatusCode)
{
throw new HttpRequestException($"Received unsuccessful status code: {(int)response.StatusCode} {response.StatusCode}");
}
MemoryStream memoryStream;
using (var downloadStream = response.Content.ReadAsStreamAsync().Result)
{
memoryStream = new MemoryStream((int)downloadStream.Length);
StreamUtils.CopyStreamWithProgress(downloadStream, memoryStream, this, DownloadProgressChanged);
}
return memoryStream;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
httpClient.Dispose();
httpRequestMessage.Dispose();
httpClient = null;
httpRequestMessage = null;
DownloadProgressChanged = null;
}
}
}
}