-
Notifications
You must be signed in to change notification settings - Fork 0
/
XmlCrb.cs
61 lines (49 loc) · 1.65 KB
/
XmlCrb.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
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System.Net.Http;
using System.Text;
using System.Xml.Linq;
using Serilog;
using Serilog.Debugging;
namespace CourseRate {
public class Crb
{
static readonly HttpClient client = new HttpClient();
string date {get; }
public Crb(string _date) {
date = _date;
}
public XDocument Xml()
{
Task<string> task = HttpResponse(date);
var text = task.GetAwaiter().GetResult();
XDocument xml = XDocument.Parse (text);
return xml;
}
static async Task<string> HttpResponse(string date)
{
string url = $"http://www.cbr.ru/scripts/XML_dynamic.asp?date_req1={date}&date_req2={date}&VAL_NM_RQ=R01235";
try
{
HttpResponseMessage response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
byte[] bytes = await response.Content.ReadAsByteArrayAsync();
Encoding encoding = Encoding.GetEncoding("utf-8");
string responseString = encoding.GetString(bytes, 0, bytes.Length);
return responseString;
}
catch(HttpRequestException e)
{
Console.WriteLine("\nException Caught!");
Console.WriteLine("Message :{0} ",e.Message);
return "";
}
}
}
}