码迷,mamicode.com
首页 > 其他好文 > 详细

Sample-Code:Translator

时间:2014-07-16 21:52:16      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   

  <h2>My Spanish Translator</h2>
       <p>
            Enter your text in English:&nbsp; </p>
       <p>
           <asp:TextBox ID="TextBox1" runat="server"  
             Width="198px"></asp:TextBox>
       </p>
     <p>
           <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
                Text="Translate" />
       </p>
       <p>
            Here is your translation:</p>
      <p>
           <asp:Literal ID="lbl1" runat="server"></asp:Literal>
      </p>

button code:

 string clientID = "<Your ClientID>";
            string clientSecret = "<Your Client Secret>";

            String strTranslatorAccessURI = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13";
            String strRequestDetails = string.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=http://api.microsofttranslator.com", HttpUtility.UrlEncode(clientID), HttpUtility.UrlEncode(clientSecret));

            System.Net.WebRequest webRequest = System.Net.WebRequest.Create(strTranslatorAccessURI);
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.Method = "POST";

            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(strRequestDetails);
            webRequest.ContentLength = bytes.Length;
            using (System.IO.Stream outputStream = webRequest.GetRequestStream())
            {
                outputStream.Write(bytes, 0, bytes.Length);
            }
            System.Net.WebResponse webResponse = webRequest.GetResponse();

            System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(AdmAccessToken));
            //Get deserialized object from JSON stream 
            AdmAccessToken token = (AdmAccessToken)serializer.ReadObject(webResponse.GetResponseStream());

            string headerValue = "Bearer " + token.access_token;

            string txtToTranslate = TextBox1.Text;
            string uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + System.Web.HttpUtility.UrlEncode(txtToTranslate) + "&from=en&to=es";
            System.Net.WebRequest translationWebRequest = System.Net.WebRequest.Create(uri);
            translationWebRequest.Headers.Add("Authorization", headerValue);
            System.Net.WebResponse response = null;
            response = translationWebRequest.GetResponse();
            System.IO.Stream stream = response.GetResponseStream();
            System.Text.Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
            System.IO.StreamReader translatedStream = new System.IO.StreamReader(stream, encode);
            System.Xml.XmlDocument xTranslation = new System.Xml.XmlDocument();
            xTranslation.LoadXml(translatedStream.ReadToEnd());
            lbl1.Text = "Your Translation is: " + xTranslation.InnerText;

AdmAccessToken Class:

  public class AdmAccessToken
       {
    
           public string access_token { get; set; }
 
          public string token_type { get; set; }
    
           public string expires_in { get; set; }
     
            public string scope { get; set; }
      }

 

Resource from: http://blogs.msdn.com/b/translation/p/gettingstarted1.aspx

Sample-Code:Translator,布布扣,bubuko.com

Sample-Code:Translator

标签:des   style   blog   http   color   os   

原文地址:http://www.cnblogs.com/akingyao/p/3835608.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!