using System; using System.Text; using System.Net; using System.IO; using System.Security.Cryptography; using System.Collections.Generic; using System.Linq; using Newtonsoft.Json; //install-package newtonsoft.json , // The 'text' parameter will contain the text from the: // - Current Clipboard when run by HotKey // - History Item when run from the History Menu // The returned string will be: // - Placed directly on the Clipboard when run as a Macro // - Ignored by ClipboardFusion if it is 'null' // - Passed along to the next action in a Trigger (null changed to an empty string) public static class ClipboardFusionHelper { public static string ProcessText(string text) { Baidu AB = new Baidu(); string retString = AB.BaiduTrans(text); if (!retString.Contains("dst")) { BFS.Dialog.ShowTrayMessage("Error:获取翻译失败!"); return text; } Root rt = JsonConvert.DeserializeObject<Root>(retString); for (int i = 0; i < rt.trans_result.Count; i++) { return(rt.trans_result[i].dst); } } } public class Trans_resultItem { public string src { get; set; } public string dst { get; set; } } public class Root { public string @from { get; set; } public string to { get; set; } public List<Trans_resultItem> trans_result { get; set; } } public class Baidu { public string BaiduTrans(string text) { string q = text; string from = "auto"; string to = "zh"; // your APP ID string appId = ""; Random rd = new Random(); string salt = rd.Next(100000).ToString(); // your key string secretKey = ""; string sign = EncryptString(appId + q + salt + secretKey); string url = "http://api.fanyi.baidu.com/api/trans/vip/translate?"; url += "q=" + WebUtility.HtmlEncode(q); url += "&from=" + from; url += "&to=" + to; url += "&appid=" + appId; url += "&salt=" + salt; url += "&sign=" + sign; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.ContentType = "text / html; charset = UTF - 8"; request.UserAgent = null; request.Timeout = 6000; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; } public string EncryptString(string str) { MD5 md5 = MD5.Create(); byte[] byteOld = Encoding.UTF8.GetBytes(str); byte[] byteNew = md5.ComputeHash(byteOld); StringBuilder sb = new StringBuilder(); foreach (byte b in byteNew) { sb.Append(b.ToString("x2")); } return sb.ToString(); } }