由于谷歌翻译官方api是付费版本,本着免费和开源的精神,分享一下用c#实现谷歌翻译api的代码。这个代码非常简单,主要分两块:通过webrequest的方式请求内容;获取get方式的请求参数(难点在于tk的获取)。
一、webrequest代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
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的获取
在打开页面是,获取到的html代码中有如下一个生成tkk的脚本:
直接运行这个脚本,可以生成一个字符串:
从监控的网络中可以发现其中一个js调用了这个tkk值,这个js加了密进行混淆的,要破解这个js需要扎实的基本功,以及足够的耐心,我也是网上找的别人破解的js代码,亲测可用,需将此代码保存在gettk.js文档中,方便调用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
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的函数,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
/// <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、实现翻译的完整代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
/// <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 ; } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/marso/p/google_translate_api.html?utm_source=tuicool&utm_medium=referral