plz no webclient in 2016

This commit is contained in:
Master Kwoth 2016-07-03 22:44:13 +02:00
parent b30a804123
commit 1b96e520e7
2 changed files with 7 additions and 6 deletions

View File

@ -6,7 +6,8 @@ using Newtonsoft.Json.Linq;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net; using System.Net.Http;
using System.Threading.Tasks;
using System.Web; using System.Web;
namespace NadekoBot.Modules.Translator.Helpers namespace NadekoBot.Modules.Translator.Helpers
@ -47,7 +48,7 @@ namespace NadekoBot.Modules.Translator.Helpers
/// <param name="sourceLanguage">The source language.</param> /// <param name="sourceLanguage">The source language.</param>
/// <param name="targetLanguage">The target language.</param> /// <param name="targetLanguage">The target language.</param>
/// <returns>The translation.</returns> /// <returns>The translation.</returns>
public string Translate public async Task<string> Translate
(string sourceText, (string sourceText,
string sourceLanguage, string sourceLanguage,
string targetLanguage) string targetLanguage)
@ -62,10 +63,10 @@ namespace NadekoBot.Modules.Translator.Helpers
GoogleTranslator.LanguageEnumToIdentifier(sourceLanguage), GoogleTranslator.LanguageEnumToIdentifier(sourceLanguage),
GoogleTranslator.LanguageEnumToIdentifier(targetLanguage), GoogleTranslator.LanguageEnumToIdentifier(targetLanguage),
HttpUtility.UrlEncode(sourceText)); 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"); 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 = wc.DownloadString(url); text = await http.GetStringAsync(url);
} }
return JArray.Parse(text)[0][0][0].ToString(); return JArray.Parse(text)[0][0][0].ToString();

View File

@ -31,7 +31,7 @@ namespace NadekoBot.Modules.Translator
if (string.IsNullOrWhiteSpace(text)) if (string.IsNullOrWhiteSpace(text))
return; return;
string translation = t.Translate(text, from, to); string translation = await t.Translate(text, from, to);
await e.Channel.SendMessage(translation).ConfigureAwait(false); await e.Channel.SendMessage(translation).ConfigureAwait(false);
} }
catch (Exception ex) catch (Exception ex)