asp.net怎么调用百度翻译,asp.net调用百度开发接口

发布时间:2019-08-10编辑:脚本学堂
asp.net调用百度翻译,实现在线翻译的方法,在页面上输入要翻译的内容,程序便会自动检测语言类型进行翻译。

asp.net调用百度翻译的方法

asp.net调用百度翻译,如下图:
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翻译-www.xingzuo51.com</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 = "请输入要翻译的内容";
}
}
}