接上篇 Google翻译API(B/S调用和C/S调用)
上篇里提到的接口调用方法是get方式,这样有个问题,每次请求翻译的内容不能超过url允许的长度。需要改成post方式才行,但是google没有提供post方式的API请求,怎么办呢?在通过网上一番资料的查找,在一位哥们的博客里看到了解决方案,不过他用的是java版的,对应post地址和参数,写出了.net版的。加上朗读的功能,程序界面如下:
/// <summary>/// Post方式获取翻译/// </summary>/// <param name="sourceText"></param>/// <param name="langPair"></param>/// <returns></returns>private static string TranslatePostMethod(string sourceText, string langPair){string fromLan = langPair; //原始语言string toLan = langPair.ToLower() == "zh" ? "en" : "zh"; //目标语言 这里只针对中英互转HttpWebRequest requestScore = (HttpWebRequest)WebRequest.Create("http://translate.google.com/translate_t#");StringBuilder postContent = new StringBuilder();Encoding myEncoding = Encoding.UTF8;postContent.Append(HttpUtility.UrlEncode("hl", myEncoding));postContent.Append("=");postContent.Append(HttpUtility.UrlEncode("en", myEncoding));postContent.Append("&");postContent.Append(HttpUtility.UrlEncode("ie", myEncoding));postContent.Append("=");postContent.Append(HttpUtility.UrlEncode("UTF-8", myEncoding));postContent.Append("&");postContent.Append(HttpUtility.UrlEncode("sl", myEncoding));postContent.Append("=");postContent.Append(HttpUtility.UrlEncode(fromLan, myEncoding));postContent.Append("&");postContent.Append(HttpUtility.UrlEncode("text", myEncoding));postContent.Append("=");postContent.Append(HttpUtility.UrlEncode(sourceText, myEncoding));postContent.Append("&");postContent.Append(HttpUtility.UrlEncode("tl", myEncoding));postContent.Append("=");postContent.Append(HttpUtility.UrlEncode(toLan, myEncoding));byte[] data = Encoding.ASCII.GetBytes(postContent.ToString());requestScore.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";requestScore.Method = "Post";//requestScore.ContentType = "application/x-www-form-urlencoded;charset=gb2312";requestScore.ContentLength = data.Length;requestScore.KeepAlive = true;requestScore.Timeout = (6 * 60 * 1000);requestScore.ProtocolVersion = HttpVersion.Version10;Stream stream = requestScore.GetRequestStream();stream.Write(data, 0, data.Length);stream.Close();string content = string.Empty;try{System.Net.ServicePointManager.Expect100Continue = false;HttpWebResponse responseSorce = (HttpWebResponse)requestScore.GetResponse();StreamReader reader = new StreamReader(responseSorce.GetResponseStream());content = reader.ReadToEnd();responseSorce.Close();reader.Dispose();stream.Dispose();}catch (WebException ex){HttpWebResponse responseSorce = (HttpWebResponse)ex.Response;//得到请求网站的详细错误提示StreamReader reader = new StreamReader(responseSorce.GetResponseStream());content = reader.ReadToEnd();responseSorce.Close();reader.Dispose();stream.Dispose();}finally{requestScore.Abort();}string reg = @"<(?<HtmlTag>[\w]+)[^>]*\s[iI][dD]=(?<Quote>[""']?)result_box(?(Quote)\k<Quote>)[""']?[^>]*>((?<Nested><\k<HtmlTag>[^>]*>)|</\k<HtmlTag>>(?<-Nested>)|.*?)*</\k<HtmlTag>>"; //返回的是一个html页面,需匹配出翻译内容Regex r = new Regex(reg);MatchCollection mcItem = r.Matches(content);string result = ConvertHtmlToText(mcItem[0].Value);return "post:" + result;}
可执行程序下载:http://download.csdn.net/source/3499399
源代码下载:http://download.csdn.net/source/3499392
参考资料:
1.Google翻译post提交无长度限制的在线翻译例子
2.使用正则表达式匹配嵌套Html标签
3.使用正则表达式将Html转换为纯文本