using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Class1 a = new Class1(); a.GoogleTranslate("我喜欢", "auto", "en"); } } }
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Web; namespace ConsoleApplication1 { public class Class1 { public string cok = "_ga=GA1.3.57119681.1530535466; _gid=GA1.3.822279719.1530535466; 1P_JAR=2018-7-2-13; NID=133=flt3w-FDEcjIeJEyNlrSsDT234uDZR5FCndtTvrkwShKHLzaTsk9FFQ0-7C9f_Qg0AgDHlVU-Z7Mz8JucebVhb2eW32XabvEEVoIJhdalWeCnkso8NUQvrBMEPT4eUJS"; public string rep = ""; public string usergant = "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"; /// <summary> /// 谷歌翻译 /// </summary> /// <param name="text">待翻译文本</param> /// <param name="fromLanguage">自动检测:auto</param> /// <param name="toLanguage">中文:zh-CN,英文:en</param> /// <returns>翻译后文本</returns> public string GoogleTranslate(string text, string fromLanguage, string toLanguage) { CookieContainer cc = new CookieContainer(); string GoogleTransBaseUrl = "https://translate.google.com/"; var BaseResultHtml = GetResultHtml(GoogleTransBaseUrl, cc, ""); Regex re = new Regex(@"(?<=TKK=)(.*?)(?=\);)"); var TKKStr = re.Match(BaseResultHtml).ToString() + ")";//在返回的HTML中正则匹配TKK的JS代码 var TKK = ExecuteScript(TKKStr, TKKStr);//执行TKK代码,得到TKK值 var GetTkkJS = File.ReadAllText("./gettk.js"); var tk = ExecuteScript("tk(\"" + text + "\",\"" + TKK + "\")", GetTkkJS); string googleTransUrl = "https://translate.google.com/translate_a/single?client=t&sl=" + fromLanguage + "&tl=" + toLanguage + "&hl=en&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&otf=1&ssel=0&tsel=0&kc=1&tk=" + tk + "&q=" + HttpUtility.UrlEncode(text); var ResultHtml = GetResultHtml(googleTransUrl, cc, "https://translate.google.com/"); // dynamic TempResult = Newtonsoft.Json.JsonConvert.DeserializeObject(ResultHtml); //string ResultText = Convert.ToString(TempResult[0][0][0]); //return ResultText; return ""; } public string GetResultHtml(string url, CookieContainer cc, string refer) { var html = ""; var webRequest = WebRequest.Create(url) as HttpWebRequest; webRequest.CookieContainer = new CookieContainer(); //加cookie CookieContainer cook = new CookieContainer(); // string[] cookstr = cok.Split(';'); // foreach (var item in cookstr) //{ //string[] cookieNameValue = item.Split('='); // Cookie ck = new Cookie(cookieNameValue[0].Trim().ToString(), cookieNameValue[1].Trim().ToString()); // ck.Domain = "https://translate.google.cn/"; // cook.Add(ck); //} webRequest.Method = "GET"; webRequest.CookieContainer = cc; webRequest.Referer = "https://translate.google.cn/"; webRequest.UserAgent = usergant; webRequest.Timeout = ; webRequest.Headers.Add("X-Requested-With:XMLHttpRequest"); webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; using (var webResponse = (HttpWebResponse)webRequest.GetResponse()) { using (var reader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8)) { html = reader.ReadToEnd(); reader.Close(); webResponse.Close(); } } return html; } /// <summary> /// 执行JS /// </summary> /// <param name="sExpression">参数体</param> /// <param name="sCode">JavaScript代码的字符串</param> /// <returns></returns> private string ExecuteScript(string sExpression, string sCode) { MSScriptControl.ScriptControl scriptControl = new MSScriptControl.ScriptControl(); scriptControl.UseSafeSubset = true; scriptControl.Language = "JScript"; scriptControl.AddCode(sCode); try { string str = scriptControl.Eval(sExpression).ToString(); return str; } catch (Exception ex) { string str = ex.Message; } return null; } } }