Asp.NET调用百度翻译,图示:

HTML:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="baidu.aspx.cs" Inherits="FanYi_baidu" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>OA翻译</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="sourceWord" runat="server" Columns="50" Rows="15" style="width:100%;"
TextMode="MultiLine"></asp:TextBox>
<br />
源语言:<asp:DropDownList ID="ddlFrom" runat="server">
<asp:ListItem Value="auto">自动检测</asp:ListItem>
<asp:ListItem Value="zh">中文</asp:ListItem>
<asp:ListItem Value="en">英文</asp:ListItem>
<asp:ListItem Value="jp">日文</asp:ListItem>
</asp:DropDownList>
目标语言:<asp:DropDownList ID="ddlTo" runat="server">
<asp:ListItem Value="auto">自动检测</asp:ListItem>
<asp:ListItem Value="zh">中文</asp:ListItem>
<asp:ListItem Value="en">英文</asp:ListItem>
<asp:ListItem Value="jp">日文</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Translate"
runat="server" Text="翻译" onclick="Translate_Click" />
<br />
<asp:TextBox ID="resultText" runat="server" TextMode="MultiLine" Rows="15" Columns="50" style="width:100%;"></asp:TextBox>
</div>
</form>
</body>
</html>

C#:

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Web; public partial class FanYi_baidu : System.Web.UI.Page
{
string url = @"http://openapi.baidu.com/public/2.0/bmt/translate";
string requestDetail = "client_id=申请的ID";
protected void Page_Load(object sender, EventArgs e)
{ }
[DataContract]
public class AdmAccessToken
{
[DataMember]
public string from { get; set; }
[DataMember]
public string to { get; set; }
[DataMember]
public string error_code { get; set; }
[DataMember]
public string error_msg { get; set; }
[DataMember]
public string query { get; set; }
[DataMember]
public List<TokenResult> trans_result { get; set; }
} [DataContract]
public class TokenResult
{
[DataMember]
public string src { get; set; }
[DataMember]
public string dst { get; set; }
} //百度翻译返回数据结构
//{
//"from": "en",
//"to": "zh",
//"trans_result": [
// {
// "src": "today",
// "dst": "今天"
// },
// {
// "src": "tomorrow",
// "dst": "明天"
// }
//],
//"error_code": "52001",
//"error_msg": "TIMEOUT",
//"query": "he's"
//} /// <summary>
/// 采用Post方式提交数据
/// </summary>
/// <param name="DatamarketAccessUri">目标网址</param>
/// <param name="requestDetails">参数字符串</param>
/// <returns></returns>
private AdmAccessToken HttpPost(string DatamarketAccessUri, string requestDetails)
{
//Prepare OAuth request
WebRequest webRequest = WebRequest.Create(DatamarketAccessUri);
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(requestDetails);
webRequest.ContentLength = bytes.Length;
using (Stream outputStream = webRequest.GetRequestStream())
{
outputStream.Write(bytes, 0, bytes.Length);
}
using (WebResponse webResponse = webRequest.GetResponse())
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(AdmAccessToken));
//Get deserialized object from JSON stream
AdmAccessToken token = (AdmAccessToken)serializer.ReadObject(webResponse.GetResponseStream());
return token;
}
} protected void Translate_Click(object sender, EventArgs e)
{
resultText.Text = "";
if (sourceWord.Text.Trim() != "")
{
string requestStr = requestDetail + "&from=" + ddlFrom.SelectedValue
+ "&to=" + ddlTo.SelectedValue
+ "&q=" + HttpUtility.UrlEncode(sourceWord.Text); AdmAccessToken token = HttpPost(url, requestStr);
if (token.error_code != null)
{
resultText.Text = token.error_msg;
}
else
{
int n = token.trans_result.Count;
for (int i = 0; i < n; i++)
{
resultText.Text += token.trans_result[i].dst + (i < n-1 ? "\n" : "");
}
}
}
else
{
resultText.Text = "请输入要翻译的内容";
}
}
}

Asp.NET调用百度翻译的更多相关文章

  1. Python 调用百度翻译API

    由于实习公司这边做的是日文app,有时要看看用户反馈,对于我这种五十音图都没记住的人,表示百度翻译确实还可以.但不想每次都复制粘贴啊,google被墙也是挺蛋疼的事,所以用python结合baidu ...

  2. Asp.net调用百度搜索引擎

    ASP.NET 调用百度搜索引擎 百度搜索引擎提供了一段嵌入到页面中的代码 <form action="http://www.baidu.com/baidu" target= ...

  3. 原生js简单调用百度翻译API实现的翻译工具

    先来个在线demo: js翻译工具 或者百度搜索js简单调用百度翻译API工具(不过有个小小的界面显示bug,我想细心的人应该会发现) 或者直接前往该网址:js翻译工具 或者前往我的github:gi ...

  4. 调用百度翻译 API 来翻译网站信息

    之前说过jquery.i18n.js 来做网站的中英翻译,前提就得做一套中文内容,一套英文内容来解决,好处是中英翻译可以准确无误,本篇文章我们来看一下调用百度翻译的 API 来进行网站的翻译,但是翻译 ...

  5. PHP 最完美调用百度翻译接口代码示例 (原)

    php调用百度翻译最新接口代码 问       题:写的过程遇到了一个问题,url拼接好的原翻译内容,appid,sign的地址直接输出到浏览器可以打开看到翻译后的返回值,但是各种curl,file_ ...

  6. jquery+asp.net 调用百度geocoder手机浏览器定位--Api介绍及Html定位方法

    原文来自:***/projecteactual/jqueryaspnetbaidugeocodermobilebrowserposition.html 在做一个社区项目中,支持移动浏览器进行选择地区和 ...

  7. .net core 和 WPF 开发升讯威在线客服系统:调用百度翻译接口实现实时自动翻译

    业余时间用 .net core 写了一个在线客服系统.并在博客园写了一个系列的文章,写介绍这个开发过程. 我把这款业余时间写的小系统丢在网上,陆续有人找我要私有化版本,我都给了,毕竟软件业的初衷就是免 ...

  8. C# 调用百度翻译Api

    这是简单的界面.用的是wpf,winform也可以 具体的操作类 public partial class MainWindow : Window { string url = "" ...

  9. 调用百度翻译API接口功能

    public string appid = "自己的APPID"; public string q = "要翻译的文本"; "; public str ...

随机推荐

  1. Spring IOC和DI原理讲解并制作LazyCoder版的Spring (一)

    转载请注意出处:http://blog.csdn.net/zcm101 写在前面的话 最近,给项目组成员培训了Spring 控制反转和依赖注入的原理,并自己做了个Lazy Coder版的Spring, ...

  2. [Swust OJ 567]--老虎在不在笼子里(凸包问题)

    题目链接:http://acm.swust.edu.cn/problem/567/ Time limit(ms): 1000 Memory limit(kb): 65535   一只老虎自从看了< ...

  3. Google Code Jam Round 1A 2015 Problem B. Haircut 二分

    Problem You are waiting in a long line to get a haircut at a trendy barber shop. The shop has B barb ...

  4. USACO Preface Numbering 构造

    一开始看到这道题目的时候,感觉好难 还要算出罗马的规则. 但是仔细一看,数据规模很小, n 只给到3500 看完题目给出了几组样例之后就有感觉了 解题方法就是: n的每个十进制数 转换成相应的罗马数字 ...

  5. (Problem 9)Special Pythagorean triplet

    A Pythagorean triplet is a set of three natural numbers, a  b  c, for which, a2 + b2 = c2 For exampl ...

  6. Xcode6项目运行在真机上未铺满整个屏幕

    如图 解决见图: 再次运行:

  7. android如何让service不被杀死-提高进程优先级

    1.在service中重写下面的方法,这个方法有三个返回值, START_STICKY是service被kill掉后自动重写创建 @Override public int onStartCommand ...

  8. bootstrap base css 基本css

    Headings All HTML headings, <h1> through <h6> are available. h1. Heading 1 h2. Heading 2 ...

  9. stm32内部的CAN总线

    功能概述: bxCAN是基本扩展CAN(Basic Extended CAN)的缩写,它支持CAN协议2.0A和2.0B:它的设计目标是以最小的CPU负载来高效处理大量的报文.它也支持报文发送的优先级 ...

  10. BZOJ 2743: [HEOI2012]采花( 离线 + BIT )

    处理出每个数下一个出现的位置, 然后按左端点排序回答询问.处理当前数去除的影响 ------------------------------------------------------------ ...