接上篇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转换为纯文本
分享到:
相关推荐
google地图API实现的一个简单demo
Android google地图api Demo。完整的最简单易懂的google地图调用案例
google官方提供的demo api19
Google官方API的demo源码.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
Google Maps API android demo Eciplse project source code and javacript version Google Maps API and Place Search example html.
GOOGLE MAP api 7个案例。 CurrentAddress 得到当前位置 gMap 地形 路线 GoogleMapDemo 当前位置 MAP 绘制路线 MapView 放大缩小 WeatherMap 在MAP上添加图片 WorldCities 城市经纬度的切换 以及 动画切换效果
谷歌翻译-api A Node.js module for working with the Google Translation. Automatically handles bulk translations that exceed the Google Translation API query limit. 这是一款基于nodejs的谷歌翻译插件,...
Google admob Demo
Java调用Google Analytics API实现网站统计demo
该Demo简单实现了,google map V2 地图的显示.
google maps API v3.0离线开发demo
android串口通信google官方demo https://code.google.com/archive/p/android-serialport-api/
GoogleMap谷歌地图demo,调用GoogleMap api 实现的功能全面的地图工具
这个是google官方提供的最新版本的4.0.3(api 15)简单的demo,也就是一些简单的api调用,对于android初学者很有帮助,直接导入eclipse,新建android项目即可使用,因为是最新版本,需要下载一个兼容包,不然会报错。...
支持谷歌、百度、有道的API翻译,将中文json文件翻译为其他语言json文件,通过Google翻译出一百多国家的语言;
今天发了半天时间才把google map 这个demo搞定,中间走了不少弯路,特意记录下,备份。
asp.net c# Google 翻译 源代码 利用Google翻译提供的api 在asp.net下实现 多国语言翻译
先前上传的资料有编译错误,今天改进了一次好了。这个是目前新Google Maps API使用的最全的DEMO。
谷歌地图使用的基础 demo 可快速掌握 基本API的使用
真实好用的javaweb上使用的googlemap的demo