From 1b96e520e7edfd48b4a18920df79b64c60165dec Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Sun, 3 Jul 2016 22:44:13 +0200 Subject: [PATCH] plz no webclient in 2016 --- .../Modules/Translator/Helpers/GoogleTranslator.cs | 11 ++++++----- NadekoBot/Modules/Translator/TranslateCommand.cs | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/NadekoBot/Modules/Translator/Helpers/GoogleTranslator.cs b/NadekoBot/Modules/Translator/Helpers/GoogleTranslator.cs index f9687033..a855b4d8 100644 --- a/NadekoBot/Modules/Translator/Helpers/GoogleTranslator.cs +++ b/NadekoBot/Modules/Translator/Helpers/GoogleTranslator.cs @@ -6,7 +6,8 @@ using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; -using System.Net; +using System.Net.Http; +using System.Threading.Tasks; using System.Web; namespace NadekoBot.Modules.Translator.Helpers @@ -47,7 +48,7 @@ namespace NadekoBot.Modules.Translator.Helpers /// The source language. /// The target language. /// The translation. - public string Translate + public async Task Translate (string sourceText, string sourceLanguage, string targetLanguage) @@ -62,10 +63,10 @@ namespace NadekoBot.Modules.Translator.Helpers GoogleTranslator.LanguageEnumToIdentifier(sourceLanguage), GoogleTranslator.LanguageEnumToIdentifier(targetLanguage), HttpUtility.UrlEncode(sourceText)); - using (WebClient wc = new WebClient()) + using (HttpClient http = new HttpClient()) { - wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"); - text = wc.DownloadString(url); + http.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"); + text = await http.GetStringAsync(url); } return JArray.Parse(text)[0][0][0].ToString(); diff --git a/NadekoBot/Modules/Translator/TranslateCommand.cs b/NadekoBot/Modules/Translator/TranslateCommand.cs index f5b70d79..5c43b8b9 100644 --- a/NadekoBot/Modules/Translator/TranslateCommand.cs +++ b/NadekoBot/Modules/Translator/TranslateCommand.cs @@ -31,7 +31,7 @@ namespace NadekoBot.Modules.Translator if (string.IsNullOrWhiteSpace(text)) return; - string translation = t.Translate(text, from, to); + string translation = await t.Translate(text, from, to); await e.Channel.SendMessage(translation).ConfigureAwait(false); } catch (Exception ex)