Esempio n. 1
0
        public async Task <string> Translate(string text, SupportedLanguage language)
        {
            var token = await this.cognitiveServicesAuthenticationService.GetToken(MicrosoftCognitiveTranslationService.AppKey);

            var client = new HttpClient();

            client.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}");
            var url = $"{MicrosoftCognitiveTranslationService.TranslateUrl}?text={text}&to={language.ToCode()}";

            var response = await client.GetAsync(url);

            if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
            {
                await this.cognitiveServicesAuthenticationService.RefreshToken(MicrosoftCognitiveTranslationService.AppKey);

                return(await this.Translate(text, language));
            }

            if (!response.IsSuccessStatusCode)
            {
                return(null);
            }

            var document = new XmlDocument();

            document.LoadXml(await response.Content.ReadAsStringAsync());

            return(document.InnerText);
        }