using System; using System.Text; using System.Net; using System.IO; using System.Runtime.Serialization.Json; using System.Runtime.Serialization; using System.Web; using System.ServiceModel.Channels; using System.ServiceModel; namespace BlogSubmitterFXApp { public static class BingTranslate { public static string Translate(string clientId,string ClientSecret,string TextForTranslate) { AdmAccessToken admToken; string headerValue; //Get Client Id and Client Secret from https://datamarket.azure.com/developer/applications/ //Refer obtaining AccessToken (http://msdn.microsoft.com/en-us/library/hh454950.aspx) AdmAuthentication admAuth = new AdmAuthentication(clientId, ClientSecret); try { admToken = admAuth.GetAccessToken(); DateTime tokenReceived = DateTime.Now; // Create a header with the access_token property of the returned token headerValue = "Bearer " + admToken.access_token; return BingTranslate.TranslateMethod(headerValue, TextForTranslate); } catch (Exception ex) { return ex.Message; } } static string TranslateMethod(string authToken, string TextForTranslate) { // Add TranslatorService as a service reference, Address:http://api.microsofttranslator.com/V2/Soap.svc TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient(); //Set Authorization header before sending the request HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty(); httpRequestProperty.Method = "POST"; httpRequestProperty.Headers.Add("Authorization", authToken); // Creates a block within which an OperationContext object is in scope. using (OperationContextScope scope = new OperationContextScope(client.InnerChannel)) { OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty; //string sourceText = "<UL><LI>Use generic class names. <LI>Use pixels to express measurements for padding and margins. <LI>Use percentages to specify font size and line height. <LI>Use either percentages or pixels to specify table and container width. <LI>When selecting font families, choose browser-independent alternatives. </LI></UL>"; string translationResult; //Keep appId parameter blank as we are sending access token in authorization header. translationResult = client.Translate("", TextForTranslate, "en", "ru", "text/html", "general"); return translationResult; } } } [DataContract] public class AdmAccessToken { [DataMember] public string access_token { get; set; } [DataMember] public string token_type { get; set; } [DataMember] public string expires_in { get; set; } [DataMember] public string scope { get; set; } } public class AdmAuthentication { public static readonly string DatamarketAccessUri = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13"; private string clientId; private string cientSecret; private string request; public AdmAuthentication(string clientId, string clientSecret) { this.clientId = clientId; this.cientSecret = clientSecret; //If clientid or client secret has special characters, encode before sending request this.request = string.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=http://api.microsofttranslator.com", HttpUtility.UrlEncode(clientId), HttpUtility.UrlEncode(clientSecret)); } public AdmAccessToken GetAccessToken() { return HttpPost(DatamarketAccessUri, this.request); } private AdmAccessToken HttpPost(string DatamarketAccessUri, string requestDetails) { //Prepare OAuth request WebRequest webRequest = WebRequest.Create(DatamarketAccessUri); webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.Method = "POST"; byte[] bytes = Encoding.ASCII.GetBytes(requestDetails); webRequest.ContentLength = bytes.Length; using (Stream outputStream = webRequest.GetRequestStream()) { outputStream.Write(bytes, 0, bytes.Length); } using (WebResponse webResponse = webRequest.GetResponse()) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(AdmAccessToken)); //Get deserialized object from JSON stream AdmAccessToken token = (AdmAccessToken)serializer.ReadObject(webResponse.GetResponseStream()); return token; } } } }
c# Переводим с помощью Bing Api translator
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: only a member of this blog may post a comment.