Asp.NET調(diào)用百度翻譯的方法
來源:易賢網(wǎng) 閱讀:1101 次 日期:2015-02-27 10:57:11
溫馨提示:易賢網(wǎng)小編為您整理了“Asp.NET調(diào)用百度翻譯的方法”,方便廣大網(wǎng)友查閱!

這篇文章主要介紹了Asp.NET調(diào)用百度翻譯的方法,是針對(duì)百度接口開發(fā)的經(jīng)典實(shí)用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了Asp.NET調(diào)用百度翻譯的方法。分享給大家供大家參考。具體分析如下:

Asp.NET調(diào)用百度翻譯,如下圖所示:

名單

Asp.NET調(diào)用百度翻譯的方法

HTML代碼如下:

代碼如下:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">

<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;

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 = @"";

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ì)有所幫助。

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:Asp.NET調(diào)用百度翻譯的方法
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)