调用百度翻译API接口功能

   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 List<string>();
            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

 

转载于:https://www.cnblogs.com/RambleLife/p/9026369.html

你可能感兴趣的:(调用百度翻译API接口功能)