C# 集成百度翻译API
using Apps.Common; using MahApps.Metro.Controls; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; using System.Web.Security; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace Apps.CodeGenerator { /// <summary> /// Translate.xaml 的交互逻辑 /// </summary> public partial class Translate : MetroWindow { public Translate() { InitializeComponent(); } private void button_Click(object sender, RoutedEventArgs e) { //http://api.fanyi.baidu.com/api/trans/vip/translate?q=apple&from=en&to=zh&appid=2015063000000001&salt=1435660288&sign=f89f9594663708c1605f3d736d01d2d4 //http://api.fanyi.baidu.com/api/trans/product/apidoc 文档 //http://api.fanyi.baidu.com/api/trans/product/desktop?req=developer 开发者中心 string query = tbQuery.Text;//翻译的语句 string from = tbFrom.Text;//从什么语言 string to = tbTo.Text;//到什么语言 string appid = "20190113000256554"; string securityKey = "XLWWaTtO2X_ZGqkbBGMS"; string salt = ResultHelper.NewTimeId; string src = appid + query + salt + securityKey; string sign = MD5Str(src); string jsonParam = RequestApi.HttpGet("http://api.fanyi.baidu.com/api/trans/vip/translate?q="+ query + "&from="+ from + "&to="+ to + "&appid="+ appid + "&salt="+ salt + "&sign="+ sign + ""); string result = ""; dynamic product = JsonHandler.Deserialize<dynamic>(jsonParam); string str = product.trans_result[0].dst; tbEN.Text = str; string[] arr = str.Split(' '); foreach (var s in arr) { result = result+ s.Substring(0, 1).ToUpper() + s.Substring(1); } result = "_"+result.Trim().Replace("-","").Replace("!", "").Replace(".", "").Replace(",", "").Replace(",", "").Replace("。", ""); jsonParam = RequestApi.HttpGet("http://api.fanyi.baidu.com/api/trans/vip/translate?q=" + query + "&from=" + from + "&to=cht&appid=" + appid + "&salt=" + salt + "&sign=" + sign + ""); product = JsonHandler.Deserialize<dynamic>(jsonParam); str = product.trans_result[0].dst; tbTW.Text = str;//输出结果 tbKey.Text = result; } public static string MD5Str(string str) { return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower(); } } }
作者:YmNets
出处:http://ymnets.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://ymnets.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。