百度翻译API
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="百度翻译_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><asp:Literal ID="Literal1" runat="server"></asp:Literal> </div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using BDTranslate.Models;
public partial class 百度翻译_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
private TranClass tranClass = new TranClass();
protected void Button1_Click(object sender, EventArgs e)
{
tranClass.From = "zh";
tranClass.To = "en";
WebClient client = new WebClient(); //引用System.Net
string fromTranslate = this.TextBox1.Text; //翻译前的内容
if (!string.IsNullOrEmpty(fromTranslate))
{
// 拼接appid=2015063000000001+q=apple+salt=1435660288+密钥=12345678
//得到字符串1 =2015063000000001apple143566028812345678
long d = DateTimeToUnixTimestamp(DateTime.Now);
string str = "key" + this.TextBox1.Text + d + "密钥"; string md5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower();
string url = "http://api.fanyi.baidu.com/api/trans/vip/translate?q=" + this.TextBox1.Text + "&from=en&to=zh&appid=key&salt="+d+"&sign=" + md5;
var buffer = client.DownloadData(url);
string result = Encoding.UTF8.GetString(buffer);
StringReader sr = new StringReader(result);
JsonTextReader jsonReader = new JsonTextReader(sr); //引用Newtonsoft.Json 自带
JsonSerializer serializer = new JsonSerializer();
var r = serializer.Deserialize<TranClass>(jsonReader); //因为获取后的为json对象 ,实行转换
Literal1.Text = r.Trans_result[].dst; //dst为翻译后的值 }
} /// <summary>
/// 日期转换成unix时间戳
/// </summary>
/// <param name="dateTime"></param>
/// <returns></returns>
public static long DateTimeToUnixTimestamp(DateTime dateTime)
{
var start = new DateTime(, , , , , , dateTime.Kind);
return Convert.ToInt64((dateTime - start).TotalSeconds);
} } namespace BDTranslate.Models
{
public class TranClass
{
public string From { get; set; }
public string To { get; set; }
public List<Trans_result> Trans_result { get; set; }
}
public class Trans_result
{
public string src { get; set; }
public string dst { get; set; }
}
}
百度翻译API的更多相关文章
- C# 调用百度翻译Api
这是简单的界面.用的是wpf,winform也可以 具体的操作类 public partial class MainWindow : Window { string url = "" ...
- 基于百度翻译API开发属于自己的翻译工具
你是否每天使用着网页翻译工具?你是否遇到过这种情况,上网过程中遇到一个很长的单词但是又不能复制,要开两个浏览器,一个打开百度翻译,照着另一个网页输入单词?你安装了各种翻译软件后,又删除,只因忍受不了那 ...
- Python 调用百度翻译API
由于实习公司这边做的是日文app,有时要看看用户反馈,对于我这种五十音图都没记住的人,表示百度翻译确实还可以.但不想每次都复制粘贴啊,google被墙也是挺蛋疼的事,所以用python结合baidu ...
- WP8.1小梦词典开发2:百度翻译API使用
原文出自:http://www.bcmeng.com/api2/ 小梦昨天和大家分享了WP8.1金山词霸API使用方法,今天继续分享windows phone 8.1中百度翻译API的使用方法.和昨天 ...
- 原生js简单调用百度翻译API实现的翻译工具
先来个在线demo: js翻译工具 或者百度搜索js简单调用百度翻译API工具(不过有个小小的界面显示bug,我想细心的人应该会发现) 或者直接前往该网址:js翻译工具 或者前往我的github:gi ...
- 百度翻译API(C#)
百度翻译开放平台:点击打开链接 1. 定义类用于保存解析json得到的结果 public class Translation { public string Src { get; set; } pub ...
- 利用百度翻译API,获取翻译结果
利用百度翻译API,获取翻译结果 translate.py #!/usr/bin/python #-*- coding:utf-8 -*- import sys reload(sys) sys.set ...
- 有道翻译 / 百度翻译Api
比较推荐使用百度翻译api 不推荐有道翻译,比较水. http://ai.youdao.com/docs/doc-trans-api.s#p02 http://ai.youdao.com/docs/d ...
- 百度翻译api 实现简易微信翻译小程序
介绍 口袋翻译 口袋翻译 微信小程序 翻译功能 含7类语言的相互翻译 包含最近10条的翻译历史回溯功能 微信搜索:简e翻译 功能展示 使用百度翻译api需要申请 appid 与 key 并在 ap ...
- Go语言使用百度翻译api
Go语言使用百度翻译api 之前做过一个使用百度翻译api的工具,这个工具用于用户的自动翻译功能,是使用C#调用百度翻译api接口,既然在学习Go语言,那必然也是要使用Go来玩耍一番.这里我是这么安排 ...
随机推荐
- 十六、Struts2文件上传与下载
文件上传与下载 1.文件上传前提:<form action="${pageContext.request.contextPath}/*" method="post& ...
- JAVA虚拟机学习笔记(一)Windows10下编译OpenJDK8
转载请注明源地址:http://www.cnblogs.com/lighten/p/5906359.html 1. 编译环境的准备 1.1 JDK源码下载 OpenJDK是JAVA发展史中的一个开源项 ...
- CentOS Linux下一个tomcat起停,查看日志的shell script
CentOS 的tomcat安装目录:/usr/local/tomcat vi MyTomcatUitl.sh 创建文件chmod u+x MyTomcatUtil.sh 赋执行 ...
- 使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(六)-- 依赖注入
本篇将介绍Asp.Net Core中一个非常重要的特性:依赖注入,并展示其简单用法. 第一部分.概念介绍 Dependency Injection:又称依赖注入,简称DI.在以前的开发方式中,层与层之 ...
- linux下最大文件数
系统级:系统级设置对所有用户有效.可通过两种方式查看系统最大文件限制1 cat /proc/sys/fs/file-max 2 sysctl -a 查看结果中fs.file-max这项的配置数量如果需 ...
- 如何验证 jemalloc 优化 Nginx 是否生效
Jemalloc 源于 Jason Evans 2006年在 BSDcan conference 发表的论文:<A Scalable Concurrent malloc Implementati ...
- mac配置iterm2和ohmyzsh
点击我直通 iTerm2下载 利用命令下载ohmyzsh curl -L http://install.ohmyz.sh | sh 安装好之后,运行 vi ~/.zshrc 可以查看到以下配置内容信息 ...
- C# 平时碰见的问题【5】
vs按F5启动调试,项目不会编译的解决办法 工具 -> 选项 -> 项目和解决方案 -> 运行时, 当项目过期(下拉框) -> 不要选[从不生成] 附英文版的:
- MVC中用ajax提交json对象数组
应用场景:在前端用ajax向服务器提交json对象数组,在controller的以对象数组作为函数的参数,提交的json数组直接转为服务器端的对象数组. 如: 要将json对象数组[{Id:1,Nam ...
- [原创]从Oracle和Microsoft Sql Server迁移到PostgreSQL Plus Advanced Server
一.了解PPAS的迁移方式1.在线迁移和离线迁移使用Migration Studio或Migration Toolkit直接向PPAS数据库进行对象定义和数据表中数据的迁移称为在线迁移,生成要迁移对象 ...