• 调用百度翻译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

  • 相关阅读:
    DelphiXE
    Delphi_检查exe文件是否是"随机基址"
    Delphi_按字节比较两个文件
    HTTP 头部
    NumberFormat usage
    tomcat 配置
    android 与JS之间的交互
    离线安装SDK
    Android SDK更新 Connection to http://dl-ssl.google.com refused
    自定义圆形图片
  • 原文地址:https://www.cnblogs.com/RambleLife/p/9026369.html
Copyright © 2020-2023  润新知