欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现谷歌翻译API示例代码

程序员文章站 2023-11-17 10:27:58
由于谷歌翻译官方api是付费版本,本着免费和开源的精神,分享一下用c#实现谷歌翻译api的代码。这个代码非常简单,主要分两块:通过webrequest的方式请求内容;获取g...

由于谷歌翻译官方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、抓包查看翻译网络请求,这里是用谷歌浏览器查看的网络请求,如下图:

C#实现谷歌翻译API示例代码

可以看到,请求方式是“get”方式,后面跟的请求参数很多,如下图:

C#实现谷歌翻译API示例代码

其中,最重要的参数有:sl--来源语言,一般设置为auto即自动检测,tl--目标语言,你想翻译成的语言,tk--ticket即使发车车票,谷歌就靠这个来防止我们免费调用的,这是本api最难的地方。

2、tk的获取

在打开页面是,获取到的html代码中有如下一个生成tkk的脚本:

C#实现谷歌翻译API示例代码

直接运行这个脚本,可以生成一个字符串:

C#实现谷歌翻译API示例代码

从监控的网络中可以发现其中一个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;

}  

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。