C#實現谷歌翻譯API示例代碼。本站提示廣大學習愛好者:(C#實現谷歌翻譯API示例代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是C#實現谷歌翻譯API示例代碼正文
作者:marso
本篇文章主要介紹了C#實現谷歌翻譯API示例代碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧由於谷歌翻譯官方API是付費版本,本著免費和開源的精神,分享一下用C#實現谷歌翻譯API的代碼。這個代碼非常簡單,主要分兩塊:通過WebRequest的方式請求內容;獲取Get方式的請求參數(難點在於tk的獲取)。
一、WebRequest代碼
var webRequest = WebRequest.Create(url) as HttpWebRequest; webRequest.Method = "GET"; webRequest.CookieContainer = cookie; webRequest.Referer = referer; webRequest.Timeout = 20000; webRequest.Headers.Add("X-Requested-With:XMLHttpRequest"); webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; webRequest.UserAgent = useragent; using (var webResponse = (HttpWebResponse)webRequest.GetResponse()) { using (var reader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8)) { html = reader.ReadToEnd(); reader.Close(); webResponse.Close(); } }
二、谷歌翻譯接口的實現
1、抓包查看翻譯網絡請求,這裡是用谷歌浏覽器查看的網絡請求,如下圖:
可以看到,請求方式是“Get”方式,後面跟的請求參數很多,如下圖:
其中,最重要的參數有:sl--來源語言,一般設置為auto即自動檢測,tl--目標語言,你想翻譯成的語言,tk--ticket即使發車車票,谷歌就靠這個來防止我們免費調用的,這是本API最難的地方。
2、tk的獲取
在打開https://translate.google.com/頁面是,獲取到的HTML代碼中有如下一個生成TKK的腳本:
直接運行這個腳本,可以生成一個字符串:
從監控的網絡中可以發現其中一個JS調用了這個TKK值,這個JS加了密進行混淆的,要破解這個JS需要扎實的基本功,以及足夠的耐心,我也是網上找的別人破解的JS代碼,親測可用,需將此代碼保存在gettk.js文檔中,方便調用:
var b = function (a, b) { for (var d = 0; d < b.length - 2; d += 3) { var c = b.charAt(d + 2), c = "a" <= c ? c.charCodeAt(0) - 87 : Number(c), c = "+" == b.charAt(d + 1) ? a >>> c : a << c; a = "+" == b.charAt(d) ? a + c & 4294967295 : a ^ c } return a } var tk = function (a,TKK) { for (var e = TKK.split("."), h = Number(e[0]) || 0, g = [], d = 0, f = 0; f < a.length; f++) { var c = a.charCodeAt(f); 128 > c ? g[d++] = c : (2048 > c ? g[d++] = c >> 6 | 192 : (55296 == (c & 64512) && f + 1 < a.length && 56320 == (a.charCodeAt(f + 1) & 64512) ? (c = 65536 + ((c & 1023) << 10) + (a.charCodeAt(++f) & 1023), g[d++] = c >> 18 | 240, g[d++] = c >> 12 & 63 | 128) : g[d++] = c >> 12 | 224, g[d++] = c >> 6 & 63 | 128), g[d++] = c & 63 | 128) } a = h; for (d = 0; d < g.length; d++) a += g[d], a = b(a, "+-a^+6"); a = b(a, "+-3^+b+-f"); a ^= Number(e[1]) || 0; 0 > a && (a = (a & 2147483647) + 2147483648); a %= 1E6; return a.toString() + "." + (a ^ h) }
要得到tk只需要,運行tk這個函數,它有兩個輸入值:a為翻譯文本內容,TKK是上文正則匹配得到的JS字符串執行的結果值。為方便在C#中執行JS,封裝了一個能執行JS的函數,如下:
/// <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; }
3、實現翻譯的完整代碼
/// <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; } public string GetResultHtml(string url,CookieContainer cc,string refer) { var html=""; var webRequest = WebRequest.Create(url) as HttpWebRequest; webRequest.Method = "GET"; webRequest.CookieContainer = cookie; webRequest.Referer = referer; webRequest.Timeout = 20000; webRequest.Headers.Add("X-Requested-With:XMLHttpRequest"); webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; webRequest.UserAgent = useragent; 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; }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。