Asp. NET call youdao translation API

Source: Internet
Author: User

Call youdao API for translation,

HTML:


 

<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "FanYi. aspx. cs" Inherits = "FanYi" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN "" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <Html xmlns =" http://www.w3.org/1999/xhtml "> <Head runat =" server "> <title> translation </title> <style type =" text/css "> # sourceWord {height: 30px; font-size: 16pt; font-weight: bold; border: 1px green solid; line-height: 30px;} # Translate {font-size: 14pt; margin-left: 10px ;} # panel_Dict {margin-top: 15px ;}. query {font-size: 16pt; color: Green; font-weight: bold ;}. phonetic {color: Gray; font-size: 13pt ;}. wordGroup {font-size: 13pt; color: #35a1d4; font-weight: bold; margin-right: 0.5em; margin-top: 10px; margin-bottom: 0px; display: block;} legend {font-size: 12pt; color: Gray;} fieldset {display: inline; padding: 10px ;} </style> 
Using System; using System. collections. generic; using System. net; using System. runtime. serialization; using System. runtime. serialization. json; using System. web; public partial class FanYi: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {translation ();} /// <summary> /// translate and display the result /// </summary> private void translation () {lblTranslation. text = ""; lblWeb. text = ""; lblQuery. te Xt = sourceWord. text; lblPhonetic. text = ""; lblExplains. text = ""; if (sourceWord. text. trim () = "") {return;} translationData data = YouDaoTranslateTool (sourceWord. text. trim (); if (data. errorCode = 0) {if (data. basic! = Null) {fieldset1.Visible = true; if (data. basic. phonetic! = "") {LblPhonetic. text = "[" + data. basic. phonetic + "]" ;}for (int I = 0; I <data. basic. explains. count; I ++) {lblExplains. text + = data. basic. explains [I] + "<br/>" ;}} else {fieldset1.Visible = false ;}for (int I = 0; I <data. translation. count; I ++) {lblTranslation. text + = data. translation [I] + "<br/>";} if (data. web! = Null) {fieldset3.Visible = true; for (int I = 0; I <data. web. count; I ++) {lblWeb. text + = "<span class = 'wordgroup'>" + data. web [I]. key + "</span>"; for (int j = 0; j <data. web [I]. value. count; j ++) {lblWeb. text + = data. web [I]. value [j] + (j = data. web [I]. value. count-1? "": ";");} LblWeb. text + = "<br/>" ;}} else {fieldset3.Visible = false ;}} protected void Translate_Click (object sender, EventArgs e) {}/// <summary> /// request Web data, returns the object class /// </summary> /// <param name = "sourceWord"> content to be translated </param> /// <returns> </returns> public translationData YouDaoTranslateTool (string sourceWord) {/* Call: <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> = Chinese */string serverUrl = @" http://fanyi.youdao.com/openapi.do?keyfrom=morninglight&key=1612199890&type=data&doctype=json&version=1.1&q= "+ HttpUtility. urlEncode (sourceWord); WebRequest request = WebRequest. create (serverUrl); WebResponse response = request. getResponse (); DataContractJsonSerializer serializer = new DataContractJsonSerializer (typeof (translationData); return (translationData) serializer. readObject (response. getResponseStream ();} # region translation result data entity class/* json data format example {"errorCode": 0 "query": "translation", "translation ": ["translation"], // youdao translation "basic": {// youdao dictionary-basic dictionary "phonetic": "f then n y then", "explains ": ["translate", "interpret"]}, "web": [// youdao dictionary-network meaning {"key": "Translation", "value ": ["translator", "translation", "translate", "Interpreter"] },{...}]} */[DataContract] public class translationData {[DataMember] public int errorCode {get; set;} [DataMember] public string query {get; set ;} [DataMember] public List <string> translation {get; set;} [DataMember] public translationBasic {get; set;} [DataMember] public List <translationWeb> web {get; set ;}} [DataContract] public class translationBasic {[DataMember] public string phonetic {get; set ;}[ DataMember] public List <string> explains {get; set ;}} [DataContract] public class translationWeb {[DataMember] public List <string> value {get; set;} [DataMember] public string key {get; set ;}# endregion}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.