.NET 4.x Кракозябры при парсинге страницы - C#
Формулировка задачи:
Получаю код страницы. С помощью регулярок извлекаю значение тэга заголовка страницы(<title>). Но в treeView выдаёт пустые строки. Посмотрев, что там не так, я увидел, что вместо русских букв получаются кракозябры. Кодировка получаемой страницы - UTF8.
Код прилагается:
Сам пытался исправить, но ничего не получается. Помогите, пожалуйста.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _2
{
public partial class Form1 : Form
{
private int a = 7713;
public Form1()
{
InitializeComponent();
}
int number = 1;
private void button1_Click(object sender, EventArgs e)
{
while (number <= 4)
{
string str = getResponse(@"https://stories.everypony.ru/story/" + number); Encoding.GetEncoding(1251);
richTextBox1.Text = str;
str = Regex.Match(str, @">([А-Я][а-я]*.[а-я]*.)([—])").ToString();
str = Regex.Match(str, @"([А-Я][а-я]*.[а-я]*)").ToString();
treeView1.Nodes.Add(str);
number = number + 1;
}
}
string getResponse(string uri)
{
try
{
StringBuilder sb = new StringBuilder();
byte[] buf = new byte[8192];
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
int count = 0;
do
{
count = resStream.Read(buf, 0, buf.Length);
if (count != 0)
{
sb.Append(Encoding.Default.GetString(buf, 0, count));
}
}
while (count > 0);
return sb.ToString();
}
catch (WebException ex)
{
HttpWebResponse webResponse = (HttpWebResponse)ex.Response;
if (webResponse.StatusCode == HttpStatusCode.NotFound)
{
return "404";
}
else
{
throw;
}
}
}
}
}Решение задачи: «.NET 4.x Кракозябры при парсинге страницы»
textual
Листинг программы
string getResponse(string uri)
{
WebClient client = new WebClient();
return client.DownloadString(uri);
}