VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之调用百度翻译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



相关教程