Asp.NET调用有道翻译API

调用有道API进行翻译,如图:

HTML:

 

[html][/html] view plaincopy

  1. <%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”FanYi.aspx.cs” Inherits=”FanYi” %>
  2. <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
  3. <html xmlns=”http://www.w3.org/1999/xhtml”>
  4. <head runat=”server”>
  5.     <title>翻译</title>
  6.     <style type=”text/css”>
  7.         #sourceWord
  8.         {
  9.             height: 30px;
  10.             font-size: 16pt;
  11.             font-weight: bold;
  12.             border: 1px green solid;
  13.             line-height: 30px;
  14.         }
  15.         #Translate
  16.         {
  17.             font-size: 14pt;
  18.             margin-left: 10px;
  19.         }
  20.         #panel_Dict
  21.         {
  22.             margin-top: 15px;
  23.         }
  24.         .query
  25.         {
  26.             font-size: 16pt;
  27.             color: Green;
  28.             font-weight: bold;
  29.         }
  30.         .phonetic
  31.         {
  32.             color: Gray;
  33.             font-size: 13pt;
  34.         }
  35.         .wordGroup
  36.         {
  37.             font-size: 13pt;
  38.             color: #35a1d4;
  39.             font-weight: bold;
  40.             margin-right: 0.5em;
  41.             margin-top: 10px;
  42.             margin-bottom: 0px;
  43.             display: block;
  44.         }
  45.         legend
  46.         {
  47.             font-size: 12pt;
  48.             color: Gray;
  49.         }
  50.         fieldset
  51.         {
  52.             display: inline;
  53.             padding: 10px;
  54.         }
  55.     </style>
  56. </head>
  57. <body>
  58.     <form id=”form1″ runat=”server”>
  59.     <div>
  60.         <asp:TextBox ID=”sourceWord” runat=”server” Style=”width: 500px;” TextMode=”SingleLine”
  61.             MaxLength=”200″></asp:TextBox>
  62.         <asp:Button ID=”Translate” runat=”server” Text=”Translate” OnClick=”Translate_Click” />
  63.         <asp:Panel ID=”panel_Dict” runat=”server”>
  64.             <asp:Label ID=”lblQuery” runat=”server” Text=”” CssClass=”query”></asp:Label>
  65.             <asp:Label ID=”lblPhonetic” runat=”server” Text=”” CssClass=”phonetic”></asp:Label>
  66.             <br />
  67.             <fieldset runat=”server” id=”fieldset1″>
  68.                 <legend>近义词</legend>
  69.                 <asp:Label ID=”lblExplains” runat=”server” Text=”” CssClass=”wordGroup”></asp:Label>
  70.             </fieldset>
  71.             <br />
  72.             <fieldset runat=”server” id=”fieldset2″>
  73.                 <legend>翻译</legend>
  74.                 <asp:Label ID=”lblTranslation” runat=”server” Text=””></asp:Label>
  75.             </fieldset>
  76.             <br />
  77.             <fieldset runat=”server” id=”fieldset3″>
  78.                 <legend>短语</legend>
  79.                 <asp:Label ID=”lblWeb” runat=”server” Text=””></asp:Label>
  80.             </fieldset>
  81.             <br />
  82.         </asp:Panel>
  83.     </div>
  84.     </form>
  85. </body>
  86. </html>

C#:

[csharp][/csharp] view plaincopy

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Runtime.Serialization;
  5. using System.Runtime.Serialization.Json;
  6. using System.Web;
  7. public partial class FanYi : System.Web.UI.Page
  8. {
  9.     protected void Page_Load(object sender, EventArgs e)
  10.     {
  11.         translation();
  12.     }
  13.     /// <summary>
  14.     /// 翻译并显示结果
  15.     /// </summary>
  16.     private void translation()
  17.     {
  18.         lblTranslation.Text = “”;
  19.         lblWeb.Text = “”;
  20.         lblQuery.Text = sourceWord.Text;
  21.         lblPhonetic.Text = “”;
  22.         lblExplains.Text = “”;
  23.         if (sourceWord.Text.Trim() == “”)
  24.         {
  25.             return;
  26.         }
  27.         translationData data = YouDaoTranslateTool(sourceWord.Text.Trim());
  28.         if (data.errorCode == 0)
  29.         {
  30.             if (data.basic != null)
  31.             {
  32.                 fieldset1.Visible = true;
  33.                 if (data.basic.phonetic != “”)
  34.                 {
  35.                     lblPhonetic.Text = “[” + data.basic.phonetic + “]”;
  36.                 }
  37.                 for (int i = 0; i < data.basic.explains.Count; i++)
  38.                 {
  39.                     lblExplains.Text += data.basic.explains[i] + “<br/>”;
  40.                 }
  41.             }
  42.             else
  43.             {
  44.                 fieldset1.Visible = false;
  45.             }
  46.             for (int i = 0; i < data.translation.Count; i++)
  47.             {
  48.                 lblTranslation.Text += data.translation[i] + “<br/>”;
  49.             }
  50.             if (data.web != null)
  51.             {
  52.                 fieldset3.Visible = true;
  53.                 for (int i = 0; i < data.web.Count; i++)
  54.                 {
  55.                     lblWeb.Text += “<span class=’wordGroup’>” + data.web[i].key + “</span>”;
  56.                     for (int j = 0; j < data.web[i].value.Count; j++)
  57.                     {
  58.                         lblWeb.Text += data.web[i].value[j] + (j == data.web[i].value.Count – 1 ? “” : “; “);
  59.                     }
  60.                     lblWeb.Text += “<br/>”;
  61.                 }
  62.             }
  63.             else
  64.             {
  65.                 fieldset3.Visible = false;
  66.             }
  67.         }
  68.     }
  69.     protected void Translate_Click(object sender, EventArgs e)
  70.     {
  71.     }
  72.     /// <summary>
  73.     /// 请求Web数据,返回实体类
  74.     /// </summary>
  75.     /// <param name=”sourceWord”>要翻译的内容</param>
  76.     /// <returns></returns>
  77.     public translationData YouDaoTranslateTool(string sourceWord)
  78.     {
  79.         /*
  80.         调用:<a href=”http://fanyi.youdao.com/openapi.do?keyfrom=?&key=?&type=data&doctype=json&version=1.1&q”>http://fanyi.youdao.com/openapi.do?keyfrom=?&key=?&type=data&doctype=json&version=1.1&q</a>=中国人
  81.         */
  82.         string serverUrl = @”http://fanyi.youdao.com/openapi.do?keyfrom=morninglight&key=1612199890&type=data&doctype=json&version=1.1&q=”
  83.         + HttpUtility.UrlEncode(sourceWord);
  84.         WebRequest request = WebRequest.Create(serverUrl);
  85.         WebResponse response = request.GetResponse();
  86.         DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(translationData));
  87.         return (translationData)serializer.ReadObject(response.GetResponseStream());
  88.     }
  89.     #region 翻译结果数据实体类
  90.         /*
  91.         json数据格式举例
  92.         {
  93.             “errorCode”:0
  94.             “query”:”翻译”,
  95.             “translation”:[“translation”], // 有道翻译
  96.             “basic”:{ // 有道词典-基本词典
  97.                 “phonetic”:”fān yì”,
  98.                 “explains”:[
  99.                     “translate”,
  100.                     “interpret”
  101.                 ]
  102.             },
  103.             “web”:[ // 有道词典-网络释义
  104.                 {
  105.                     “key”:”翻译”,
  106.                     “value”:[“translator”,”translation”,”translate”,”Interpreter”]
  107.                 },
  108.                 {…}
  109.             ]
  110.         }
  111.         */
  112.     [DataContract]
  113.     public class translationData
  114.     {
  115.         [DataMember]
  116.         public int errorCode { get; set; }
  117.         [DataMember]
  118.         public string query { get; set; }
  119.         [DataMember]
  120.         public List<string> translation { get; set; }
  121.         [DataMember]
  122.         public translationBasic basic { get; set; }
  123.         [DataMember]
  124.         public List<translationWeb> web { get; set; }
  125.     }
  126.     [DataContract]
  127.     public class translationBasic
  128.     {
  129.         [DataMember]
  130.         public string phonetic { get; set; }
  131.         [DataMember]
  132.         public List<string> explains { get; set; }
  133.     }
  134.     [DataContract]
  135.     public class translationWeb
  136.     {
  137.         [DataMember]
  138.         public List<string> value { get; set; }
  139.         [DataMember]
  140.         public string key { get; set; }
  141.     }
  142.     #endregion
  143. }

标签