有道云官方文档
官方接口:
有道云翻译API简介:
有道云C#Demo :
查询单词信息
在有道云的Demo中,已经很完整的给出了相应的代码~
但是针对是的英-汉等翻译,如何只查询单个单词的详细信息,包含单词的翻译/音标/释义等?例如下图示例信息:
下面修改后封装的查询单词详细信息服务(仅供参考):
1 ///2 /// 有道词典API 3 /// 4 internal class YouDaoApiService 5 { 6 const string AppKey = "131b76a4ee1ecd13";//AppKey和AppSecret是本人@Winter申请的账号,仅供测试使用 7 const string LangEn = "en"; 8 const string AppSecret = "KX9hLrgSMhfKkvIqS6nhwtwMcRymJqEA"; 9 10 public static async TaskGetTranslatioAsync(string queryText, string from = LangEn, string to = LangEn)11 {12 var requestUrl = GetRequestUrl(queryText, from, to);13 14 WebRequest translationWebRequest = WebRequest.Create(requestUrl);15 16 var response = await translationWebRequest.GetResponseAsync();17 18 using (Stream stream = response.GetResponseStream())19 {20 using (StreamReader reader = new StreamReader(stream ?? throw new InvalidOperationException("有道Api查询出错!"), Encoding.GetEncoding("utf-8")))21 {22 string result = reader.ReadToEnd();23 var youDaoTranslationResponse = JsonConvert.DeserializeObject (result);24 25 return youDaoTranslationResponse;26 }27 }28 }29 30 private static string GetRequestUrl(string queryText, string from, string to)31 {32 string salt = DateTime.Now.Millisecond.ToString();33 34 MD5 md5 = new MD5CryptoServiceProvider();35 string md5Str = AppKey + queryText + salt + AppSecret;36 byte[] output = md5.ComputeHash(Encoding.UTF8.GetBytes(md5Str));37 string sign = BitConverter.ToString(output).Replace("-", "");38 39 var requestUrl = string.Format(40 "http://openapi.youdao.com/api?appKey={0}&q={1}&from={2}&to={3}&sign={4}&salt={5}",41 AppKey,42 HttpUtility.UrlDecode(queryText, System.Text.Encoding.GetEncoding("UTF-8")),43 from, to, sign, salt);44 45 return requestUrl;46 }47 }
注:值得一提的是,查询单词信息,en->en路径有道提供的数据不完整,会返回301错误码。联系有道开发后,提供的方案是auto->zhs。
序列化解析的数据类:
1 [DataContract] 2 public class YouDaoTranslationResponse 3 { 4 [DataMember(Name = "errorCode")] 5 public string ErrorCode { get; set; } 6 7 [DataMember(Name = "query")] 8 public string QueryText { get; set; } 9 10 [DataMember(Name = "speakUrl")]11 public string InputSpeakUrl { get; set; }12 13 [DataMember(Name = "tSpeakUrl")]14 public string TranslationSpeakUrl { get; set; }15 16 ///17 /// 首选翻译18 /// 19 [DataMember(Name = "translation")]20 public ListFirstTranslation { get; set; }21 22 /// 23 /// 基本释义24 /// 25 [DataMember(Name = "basic")]26 public TranslationBasicData BasicTranslation { get; set; }27 28 /////29 ///// 网络释义,该结果不一定存在,暂时不使用30 ///// 31 //[DataMember(Name = "web")]32 //public TranslationWebData WebTranslation { get; set; }33 }34 35 ///36 /// 基本释义37 /// 38 [DataContract]39 public class TranslationBasicData40 {41 [DataMember(Name = "phonetic")]42 public string Phonetic { get; set; }43 44 ///45 /// 英式发音46 /// 47 [DataMember(Name = "uk-phonetic")]48 public string UkPhonetic { get; set; }49 50 ///51 /// 美式发音52 /// 53 [DataMember(Name = "us-phonetic")]54 public string UsPhonetic { get; set; }55 56 ///57 /// 翻译58 /// 59 [DataMember(Name = "explains")]60 public ListExplains { get; set; }61 }62 63 /// 64 /// 网络释义65 /// 66 [DataContract]67 public class TranslationWebData68 {69 [DataMember(Name = "key")]70 public string Key { get; set; }71 72 [DataMember(Name = "value")]73 public ListExplains { get; set; }74 }
以上Demo: