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();
        }
    }
}
复制代码

 

posted @   ymnets  阅读(653)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示