调用百度翻译API接口功能

匿名 (未验证) 提交于 2019-12-02 22:10:10
   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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!