Asp.NET中怎么调用百度翻译-创新互联
Asp.NET中怎么调用百度翻译,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
成都创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站设计、网站建设、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的澄海网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!Asp.NET调用百度翻译,如下图所示:
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=申请的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; } } //百度翻译返回数据结构 //{ //"from": "en", //"to": "zh", //"trans_result": [ // { // "src": "today", // "dst": "今天" // }, // { // "src": "tomorrow", // "dst": "明天" // } //], //"error_code": "52001", //"error_msg": "TIMEOUT", //"query": "he's" //} /// /// 采用Post方式提交数据 /// /// 目标网址 /// 参数字符串 ///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中怎么调用百度翻译-创新互联
转载源于:http://myzitong.com/article/ddjjsh.html