Сменить кодировку текста пр GET-запросе - C#
Формулировка задачи:
Метод возвращает кракозябры.
Использую метод ConvertMsg.
public static string _GET(string url) // GET запрос
{
try
{
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
System.Net.WebRequest reqGET = System.Net.WebRequest.Create(url);
System.Net.WebResponse resp = reqGET.GetResponse();
System.IO.Stream stream = resp.GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(stream);
string html = sr.ReadToEnd();
return html;
}
catch { }
}
public static string moreAnekdot(int service)
{
string result = "";
XmlDocument xml = new XmlDocument();
xml.LoadXml(_GET("http://rzhunemogu.ru/Rand.aspx?CType=" + service));
XmlNodeList first = xml.SelectNodes("root/content");
foreach (XmlNode url in first)
{
result = url.InnerText;
}
/* Так тоже кракозябры
var fromEncodind = Encoding.UTF8;
var bytes = fromEncodind.GetBytes(result);
var toEncoding = Encoding.GetEncoding("windows-1251");
result = toEncoding.GetString(bytes);*/
return result;
}
public string ConvertMsg() // и вот тут возвращает в кракозябрах
{
string original = textBox_message.Text; // textBox
string converted = original.Replace("{anekdot}", Methods.moreAnekdot(1));
converted = converted.Replace("{quota}", Methods.moreAnekdot(5));
converted = converted.Replace("{anekdot_18}", Methods.moreAnekdot(11));
converted = converted.Replace("{quota_18}", Methods.moreAnekdot(15));
converted = Encoding.Default.GetString(bytes);
return converted;
}Решение задачи: «Сменить кодировку текста пр GET-запросе»
textual
Листинг программы
System.IO.StreamReader sr = new System.IO.StreamReader(stream, Encoding.GetEncoding(1251));