Asp.NET中怎么調(diào)用百度翻譯,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
成都創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的澄海網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!Asp.NET調(diào)用百度翻譯,如下圖所示:
HTML代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="baidu.aspx.cs" Inherits="FanYi_baidu" %>OA翻譯
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=申請(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 Listtrans_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" //} /// /// 采用Post方式提交數(shù)據(jù) /// /// 目標(biāo)網(wǎng)址 /// 參數(shù)字符串 ///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)容"; } } }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。