调用百度翻译API接口功能

2021-04-11 13:24

阅读:603

标签:翻译   stc   key   obj   service   put   自己的   code   col   

   public string appid = "自己的APPID";
        public string q = "要翻译的文本";
        public string salt = "1435660288";
        public string key = "你的密钥";
        public string from = "你的源语言例如zh";
        public string to = "要翻译成的语言例如en";
        public string GetJson()
        {
            var client = new RestClient("http://api.fanyi.baidu.com");
            var request = new RestRequest("/api/trans/vip/translate", Method.GET);
            request.AddParameter("q", q);
            request.AddParameter("from", from);
            request.AddParameter("to", to);
            request.AddParameter("appid", appid);
            request.AddParameter("salt", salt);
            request.AddParameter("sign", getMd5());
            IRestResponse response = client.Execute(request);
            return response.Content;
        }
        public string sign
        {
            get { return string.Format("{0}{1}{2}{3}", appid, q, salt, key); }
        }
        string getMd5()
        {
            var md5 = new MD5CryptoServiceProvider();
            var result = Encoding.UTF8.GetBytes(sign);
            var output = md5.ComputeHash(result);
            return BitConverter.ToString(output).Replace("-", "").ToLower();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            textBox2.Text = GetResult();
        }
        public string GetResult()
        {
            var lst = new Liststring>();
            var content = GetJson();
            dynamic json = JsonConvert.DeserializeObject(content);
            foreach (var item in json.trans_result)
            {
                lst.Add(item.dst.ToString());
            }
            return string.Join(";", lst);
        }

要引用Newtonsoft.Json.dll

https://download.csdn.net/download/jsqdragoon/10032906?web=web

 

调用百度翻译API接口功能

标签:翻译   stc   key   obj   service   put   自己的   code   col   

原文地址:https://www.cnblogs.com/RambleLife/p/9026369.html


评论


亲,登录后才可以留言!