Asp.NET調(diào)用百度翻譯的方法
本文實(shí)例講述了Asp.NET調(diào)用百度翻譯的方法。分享給大家供大家參考。具體分析如下:
Asp.NET調(diào)用百度翻譯,如下圖所示:
HTML代碼如下:
<!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">自動(dòng)檢測(cè)</asp:ListItem>
<asp:ListItem Value="zh">中文</asp:ListItem>
<asp:ListItem Value="en">英文</asp:ListItem>
<asp:ListItem Value="jp">日文</asp:ListItem>
</asp:DropDownList>
目標(biāo)語言:<asp:DropDownList ID="ddlTo" runat="server">
<asp:ListItem Value="auto">自動(dòng)檢測(cè)</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.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=申請(qǐng)的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; }
}
//百度翻譯返回?cái)?shù)據(jù)結(jié)構(gòu)
//{
//"from": "en",
//"to": "zh",
//"trans_result": [
// {
// "src": "today",
// "dst": "今天"
// },
// {
// "src": "tomorrow",
// "dst": "明天"
// }
//],
//"error_code": "52001",
//"error_msg": "TIMEOUT",
//"query": "he's"
//}
/// <summary>
/// 采用Post方式提交數(shù)據(jù)
/// </summary>
/// <param name="DatamarketAccessUri">目標(biāo)網(wǎng)址</param>
/// <param name="requestDetails">參數(shù)字符串</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 = "請(qǐng)輸入要翻譯的內(nèi)容";
}
}
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
- asp.net c# 調(diào)用百度pai實(shí)現(xiàn)在線翻譯,英文轉(zhuǎn)中文
- ASP.NET 調(diào)用百度搜索引擎的代碼
- ASP.NET中實(shí)現(xiàn)獲取調(diào)用方法名
- ASP.NET中MVC使用AJAX調(diào)用JsonResult方法并返回自定義錯(cuò)誤信息
- Winform實(shí)現(xiàn)調(diào)用asp.net數(shù)據(jù)接口實(shí)例
- asp.net中IDataParameter調(diào)用存儲(chǔ)過程的實(shí)現(xiàn)方法
- 使用asp.net調(diào)用谷歌地圖api示例
- asp.net調(diào)用飛信免費(fèi)發(fā)短信(測(cè)試有效)
相關(guān)文章
.NET Core創(chuàng)建一個(gè)控制臺(tái)(Console)程序
這篇文章主要為大家詳細(xì)介紹了.NET Core如何創(chuàng)建一個(gè)控制臺(tái)程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04Excel、記事本數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫的實(shí)現(xiàn)方法
將手機(jī)號(hào)批量導(dǎo)入數(shù)據(jù)庫。思路:先將要導(dǎo)入的文件傳上項(xiàng)目里,然后讀取文件的每行數(shù)據(jù)并插入數(shù)據(jù)庫,操作完后再將上傳的文件刪除2013-10-10AspNetPager與Socut.Data使用實(shí)例代碼
最近對(duì)AspNetPager與Socut.Data這兩個(gè)控件產(chǎn)生了濃厚的興趣,這兩個(gè)控件配合可以減輕很多程序員編寫代碼的壓力。ASpNetPager為分頁控件,而Socut.Data為數(shù)據(jù)操作控件,ACCESS,MSSQL都可以。2008-07-07asp.net通過HttpModule自動(dòng)在Url地址上添加參數(shù)
由于項(xiàng)目中有許多頁面需要用到cid參數(shù),所以想通過傳值cid來獲取數(shù)據(jù)。2010-01-01ASP.Net Core(C#)創(chuàng)建Web站點(diǎn)的實(shí)現(xiàn)
本文主要介紹了ASP.Net Core(C#)創(chuàng)建Web站點(diǎn)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Extjs4.1.x 框架搭建 采用Application動(dòng)態(tài)按需加載MVC各模塊完美實(shí)現(xiàn)
中午的時(shí)候發(fā)了第一篇 Extjs4.1.x 框架搭建 采用Application動(dòng)態(tài)按需加載MVC各模塊,發(fā)現(xiàn)實(shí)現(xiàn)上還是有問題,本文將提供詳細(xì)的完美方案2012-11-11