-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTMLSourceLoader.cs
More file actions
64 lines (51 loc) · 1.55 KB
/
HTMLSourceLoader.cs
File metadata and controls
64 lines (51 loc) · 1.55 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
using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Threading;
namespace HTMLSourceViewer
{
public class HTMLSourceLoader
{
public const int ProgressTimeout = 50;
private readonly Printer printer;
public HTMLSourceLoader(Printer printer)
{
this.printer = printer;
}
public async Task<Result> LoadSource(string url)
{
var result = new Result();
using (var client = new HttpClient())
{
try
{
result.HTMLSource = await LoadSource(client, url);
}
catch (Exception e)
{
result.IsError = true;
result.ErrorMesssage = e.Message;
}
}
return result;
}
private async Task<string> LoadSource(HttpClient client, string url)
{
var getResponse = client.GetAsync(url);
WaitForResponse(getResponse);
var response = await getResponse;
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
private void WaitForResponse(Task<HttpResponseMessage> getResponse)
{
printer.PrintLoadingBegin();
while (!getResponse.IsCompleted)
{
printer.PrintLoadingProgress();
Thread.Sleep(ProgressTimeout);
}
printer.PrintLoadingEnd();
}
}
}