by DJ_miXxXer

 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
using System.Xml;
namespace Lab6 {
public partial class FormMain: Form {
public FormMain() {
InitializeComponent();
}
private void FormMain_Load(object sender, EventArgs e) {
this.Text = "Курс валют";
}
private void buttonRefresh_Click(object sender, EventArgs e) {
((Button)sender).Enabled = false;
TcpClient tcp = new TcpClient("nbrb.by", 80);
NetworkStream ns = tcp.GetStream();
StreamReader sr = new StreamReader(ns);
StreamWriter sw = new StreamWriter(ns);
String request = "GET /Services/XmlExRates.aspx?ondate= HTTP/1.0\r\nUser-agent: bot\r\nAccept:*.*\r\nHost:nbrb.by\r\n\r\n";
sw.Write(request);
sw.Flush();
string output = sr.ReadToEnd();
StringBuilder _output = new StringBuilder();
if (output.IndexOf("HTTP/1.1 200 OK") > -1) {
output = output.Substring(output.IndexOf("\r\n\r\n") + 4);
output = output.Replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", "");
using (XmlReader reader = XmlReader.Create(new StringReader(output))) {
while (reader.Read()) {
switch (reader.Name) {
case "Scale":
_output.Append(reader.ReadString() + " ");
break;
case "Name":
_output.Append(reader.ReadString() + " за ");
break;
case "Rate":
_output.Append(reader.ReadString() + " рублей\r\n");
break;
}
}
}
} else {
MessageBox.Show("Ошибка!");
}
textBox.Text = _output.ToString();
ns.Close();
tcp.Close();
((Button)sender).Enabled = true;
}
}
}