Remove the last of WebClient

This commit is contained in:
niksedk 2022-08-19 07:27:02 +02:00
parent 43b36e3886
commit d0c9ecfb8c
2 changed files with 20 additions and 20 deletions

View File

@ -3,6 +3,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
@ -13,8 +14,18 @@ namespace Nikse.SubtitleEdit.Core.Translate.Service
/// </summary>
public class GoogleTranslator1 : ITranslationStrategy
{
private readonly HttpClient _httpClient;
private const char SplitChar = '\n';
public GoogleTranslator1()
{
_httpClient = HttpClientHelper.MakeHttpClient();
_httpClient.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");
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=UTF-8");
_httpClient.BaseAddress = new Uri("https://translate.googleapis.com/");
}
public string GetName()
{
return "Google Translate V1 API";
@ -37,7 +48,6 @@ namespace Nikse.SubtitleEdit.Core.Translate.Service
public List<string> Translate(string sourceLanguage, string targetLanguage, List<Paragraph> sourceParagraphs)
{
string jsonResultString;
var input = new StringBuilder();
var formatList = new List<Formatting>();
@ -59,23 +69,15 @@ namespace Nikse.SubtitleEdit.Core.Translate.Service
try
{
using (var wc = new WebClient())
{
string url = $"https://translate.googleapis.com/translate_a/single?client=gtx&sl={sourceLanguage}&tl={targetLanguage}&dt=t&q={Utilities.UrlEncode(input.ToString())}";
wc.Proxy = Utilities.GetProxy();
wc.Encoding = Encoding.UTF8;
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");
jsonResultString = wc.DownloadString(url).Trim();
}
var url = $"translate_a/single?client=gtx&sl={sourceLanguage}&tl={targetLanguage}&dt=t&q={Utilities.UrlEncode(input.ToString())}";
jsonResultString = _httpClient.GetStringAsync(url).Result;
}
catch (WebException webException)
{
throw new TranslationException("Free API quota exceeded?", webException);
}
List<string> resultList = ConvertJsonObjectToStringLines(jsonResultString);
var resultList = ConvertJsonObjectToStringLines(jsonResultString);
resultList = ProcessPostFormatting(resultList, targetLanguage, formatList);
//brummochse: I do not really understand under which circumstances the following code is executed and if it is still required or maybe obsolete?

View File

@ -19,20 +19,16 @@ namespace Nikse.SubtitleEdit.Core.Translate.Service
private const string TranslateUrl = "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&from={0}&to={1}";
private const string SecurityHeaderName = "Ocp-Apim-Subscription-Key";
private static List<TranslationPair> _translationPairs;
private string _accessToken;
private readonly string _accessToken;
private readonly string _category;
private readonly string _apiKey;
private readonly string _tokenEndpoint;
public MicrosoftTranslationService(string apiKey, string tokenEndpoint, string category)
{
_apiKey = apiKey;
_tokenEndpoint = tokenEndpoint;
_category = category; // Optional parameter - used to get translations from a customized system built with Custom Translator
try
{
_accessToken = GetAccessToken(_apiKey, _tokenEndpoint);
_accessToken = GetAccessToken(apiKey, tokenEndpoint);
}
catch (Exception e)
{
@ -62,9 +58,11 @@ namespace Nikse.SubtitleEdit.Core.Translate.Service
return _translationPairs;
}
using (var wc = new WebClient { Proxy = Utilities.GetProxy(), Encoding = Encoding.UTF8 })
using (var httpClient = HttpClientHelper.MakeHttpClient())
{
var json = wc.DownloadString(LanguagesUrl);
httpClient.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");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=UTF-8");
var json = httpClient.GetStringAsync(LanguagesUrl).Result;
_translationPairs = FillTranslationPairsFromJson(json);
return _translationPairs;
}