public string AzureTranslateText(string subscriptionKey, string text, string sourceLanguage, string destinationLanguage)
        {
            string             uri;
            var                authTokenSource = new AzureAuthToken(subscriptionKey.Trim());
            string             authToken       = authTokenSource.GetAccessToken();
            HttpRequestMessage request;

            if (sourceLanguage == "")
            {
                uri     = "https://api.microsofttranslator.com/v2/Http.svc/Detect?text=" + text;
                request = new HttpRequestMessage(HttpMethod.Get, uri);

                request.Headers.Add("Authorization", authToken);

                XmlDocument xmlDoc = new XmlDocument();

                xmlDoc.LoadXml(ExecuteAsyncRequest(request));
                sourceLanguage = xmlDoc.ChildNodes[0].InnerText;
            }

            uri = "https://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + text + "&from=" + sourceLanguage + "&to=" + destinationLanguage;

            request = new HttpRequestMessage(HttpMethod.Get, uri);
            request.Headers.Add("Authorization", authToken);

            return(ExecuteAsyncRequest(request));
        }
示例#2
0
        public string AzureTranslateText(string subscriptionKey, string text, string sourceLanguage, string destinationLanguage)
        {
            string         uri;
            string         result          = "";
            var            authTokenSource = new AzureAuthToken(subscriptionKey.Trim());
            string         authToken       = authTokenSource.GetAccessToken();
            HttpWebRequest req;

            if (sourceLanguage == "")
            {
                uri = "https://api.microsofttranslator.com/v2/Http.svc/Detect?text=" + text;
                req = HttpWebRequest.Create(uri) as HttpWebRequest;
                req.Headers.Add("Authorization", authToken);
                using (StreamReader sr = new StreamReader(req.GetResponse().GetResponseStream()))
                {
                    XmlDocument xmlDoc = new XmlDocument();

                    result = sr.ReadToEnd();
                    xmlDoc.LoadXml(result);
                    sourceLanguage = xmlDoc.ChildNodes[0].InnerText;
                }
            }



            uri = "https://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + text + "&from=" + sourceLanguage + "&to=" + destinationLanguage;

            req = HttpWebRequest.Create(uri) as HttpWebRequest;
            req.Headers.Add("Authorization", authToken);
            //req.Accept = "application/json";
            // req.Method = "POST";
            // req.ContentType = "application/json";
            //req.ContentLength = data.Length;

            // req.GetRequestStream().Write(data, 0, data.Length);

            using (StreamReader sr = new StreamReader(req.GetResponse().GetResponseStream()))
            {
                result = sr.ReadToEnd();
            }


            return(result);
        }