-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathBlog.cs
More file actions
38 lines (33 loc) · 978 Bytes
/
Blog.cs
File metadata and controls
38 lines (33 loc) · 978 Bytes
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
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
namespace HttpClientConsoleSample
{
public interface IBlog
{
Task<string> GetArticle(string article);
}
public class Blog : IBlog
{
private readonly IHttpClientFactory _client;
public Blog(IHttpClientFactory client)
{
_client = client;
}
public async Task<string> GetArticle(string article)
{
var uri = Path.Combine("https://blog.kkbruce.net/", article);
var request = new HttpRequestMessage(HttpMethod.Get, uri);
var client = _client.CreateClient();
var response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadAsStringAsync();
}
else
{
return $"StatusCode: {response.StatusCode}";
}
}
}
}