Sample-Code:Translator

  <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

时间: 2024-11-03 20:57:45

Sample-Code:Translator的相关文章

如何将经纬度利用Google Map API显示C# VS2005 Sample Code

原文 如何将经纬度利用Google Map API显示C# VS2005 Sample Code 日前写了一篇如何用GPS抓取目前所在,并回传至资料库储存,这篇将会利用这些回报的资料,将它显示在地图上,这个做法有两种,最简单的就是直接传值到Google Maps上. 举例来说,当我们知道经纬度后,只要将数据套到以下网址即可. http://maps.google.com/maps?q=25.048346%2c121.516396 在参数q=后面,就可以加上经纬度了. 25.048346是Lati

Compilation of OpenGL Redbook sample code

http://download.csdn.net/detail/gflytu/4110817#comment [email protected]:~/Downloads/redbook$ gcc -lglut -lGL -lGLU aaindex.c aaindex.c:(.text+0x2f7): undefined reference to `glutInit'aaindex.c:(.text+0x303): undefined reference to `glutInitDisplayMo

sample code /calculate PI (转)

using System; using System.Math; namespace PiWithMonteCarlo { /// <summary> /// Trivial, synchronous calculation algorithm /// </summary> public static class TrivialPiCalculator { public static double Calculate(int iterations) { int inCircle =

sqoop sample code

本文使用的数据库是mysql的sample database employees. download url:https://launchpad.net/test-db/employees-db-1/1.0.6 然后根据ReadMe安装到自己的mysql数据库中. sqoop的安装: 下载地址:http://apache.dataguru.cn/sqoop/1.4.6/ sqoop-1.4.6.bin__hadoop-2.0.4-alpha.tar.gz sqoop-1.4.6.tar.gz 我

lua sample code analysis

What is a meta table a meta table has a __name attr whose value is name of metatable a meta table is stored in LUA_REGISTRYINDEX whose key is its name Code analysis Appl DUMP_STACK(L); /*{ "foo", "C:\\jshe\\codes\\mylualib\\test\\../build/v

使用MD5將字串加密 C# VS2005 Sample Code

MD5的加密已經出來很長一段時間了,也不是什麼特別的新技術,寫這篇的用意也有點像是給自己的一個Note,畢竟加密的功能不常用,最多寫成一個Class,未來去呼叫就好,怕自己也會忘記,所以把這個寫下來. 初步的UI設定如下: H執行是單純加密,而Salt執行則是跑Salted Hash的動作. 單純加密比較不好,如果被人猜到加密方法是採MD5,也沒有Salted,那會風險會高一點,而Salt的效果,因為多了一個Value去加密,除了前面的都要猜到外,還要知道Salted Value才可以.而Sal

python参数Sample Code

import time import datetime import getopt import sys try: opts, args = getopt.getopt(sys.argv[1:], "ho:", ["inputOCR=", "inputSpeech="]) except getopt.GetoptError: print ('Getopt Error!') sys.exit(1) for name, value in opts:

Stylecop code sample

StyleCopanalyzes C# source code to enforce a set of style and consistency rules. It can be run from inside of Visual Studio or integrated into an MSBuild project. StyleCop has also been integrated into many third-party development tools. StyleCop inc

solrcloud sample

在solrcloud出来之前,如果通过solrj连接solrserver,需要程序自己实现一致性hash.新版本的solr支持cloud的部署方式,可以自动实现lb和sharding的功能(通过CloudSolrServer类连接cloud),可以用下面代码做测试需要的jar包如下: apache-solr-solrj.jar apache-solr-core.jar zookeeper.jar    commons-logging.jar  apache-logging-log4j.jar 

Don’t Put View Code Into Your View Controller别把View创建的代码放在VC中(swift)

Don't Put Into Your View Controller别把View创建的代码放在VC中html, body {overflow-x: initial !important;}.CodeMirror { height: auto; } .CodeMirror-scroll { overflow-y: hidden; overflow-x: auto; } .CodeMirror-lines { padding: 4px 0px; } .CodeMirror pre { paddin