接上篇 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相关推荐

  1. 在C#中使用Google翻译API

    目录 介绍 要求 示例翻译函数 兴趣点 介绍 我们想轻松地将文本字符串翻译成另一种语言.从Google 翻译API返回的结果非常晦涩.它们采用JSON锯齿状数组的形式.当您必须翻译多个句子时,甚至更加 ...

  2. js 使用百度翻译api demo

    js 使用百度翻译api demo 详情请看官网 https://api.fanyi.baidu.com/ 我所使用的是通用翻译api 各语言: <!doctype html> <h ...

  3. Google翻译API(B/S调用和C/S调用)

    浏览器调用: <html><head><script type="text/javascript" src="http://www.goog ...

  4. Google翻译API的使用

    今天学习了下Google翻译的API,有所收获,也有些问题.      总结了下,主要有两种调用方式:B/S方式(在网页中),C/S方式(在Java程序中). 第一种:B/S方式. Google Aj ...

  5. Google翻译API使用案例

    简单整理了下Google翻译的API使用方法.简单的说就是一个Ajax请求: 效果图: 源代码:都附上了注释还是比较好看懂的.将该代码复制成html即可运行. <html> <hea ...

  6. c# 使用百度通用翻译api DEMO 报错

    报错 52003 和 54001 原因出在DEMO默认加上了domain //domainstring domain = "medicine";string sign = Encr ...

  7. python 调用Google Translate API进行翻译

    调用Google 翻译API进行翻译 #!/usr/bin/python #coding: UTF-8 import sys reload(sys) sys.setdefaultencoding('u ...

  8. Android google翻译踩坑之旅

    # Android google翻译踩坑之旅   最近由于工作需求,需要为游戏Android平台接入Google翻译的SDK,由于关于翻译的文章非常少,访问官方文档又需要翻墙,更可气的是找到的博客写了 ...

  9. 微软翻译api的使用介绍和注意事项

    google翻译api已经收费了,而微软翻译api目前是免费的,支持几种不同的方式访问,如果感兴趣可以自己封装下协议处理.官方介绍: http://msdn.microsoft.com/en-us/l ...

最新文章

  1. php 刀客友朋,数组与数组函数学习心得
  2. flask+sqlite3+echarts3+ajax 异步数据加载
  3. php和mysql的概述_PHP的MySQL扩展:MySQL数据库概述_MySQL
  4. ZooKeeper入门(四)实现分布式锁
  5. 这样去写你的 HTML
  6. 矩阵论-范数理论及其应用
  7. HDFS SnapShot原理
  8. MongoDB初探系列之二:认识MongoDB提供的一些经常使用工具
  9. java图形化元件竖直排列_Java:图形化比较排序
  10. 文字处理技术:布局绘制的方向有哪些?
  11. 公开的Webservice集锦
  12. 《商务与经济统计》要点回顾笔记
  13. 使用UCDOS的HZK16点阵字库的演示程序
  14. java重载父类方法_子类重载父类方法
  15. 主流编程语言的特点以及优缺点(一)
  16. java 以2为底的对数_Java对数函数及Java对数运算
  17. mysql 脏读 不可重复读 幻读_mysql事务隔离级别/脏读/不可重复读/幻读详解
  18. MySQL高级-04-授课笔记
  19. kotlin coroutine源码解析之Job启动流程
  20. SQLMap使用|命令大全(干货)

热门文章

  1. Eclipse插件开发----------File转换为IFile
  2. Git Bash中怎么复制与粘贴
  3. CSP拼图问题--矩阵快速幂
  4. 图解Java多线程设计模式——Java多线程基础
  5. 树莓派——win10远程登录以及蓝屏问题(转载)
  6. 解决HTML中文乱码
  7. MATLAB激活了还需要激活
  8. 【C++学习】Effective C++
  9. 【光线追踪系列九】物体动态模糊
  10. 海明码检错与纠错,经典例子讲解~