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 translation </title>
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 = requested 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] p Ublic List <TokenResult> trans_result {get; set ;}} [DataContract] public class TokenResult {[DataMember] public string src {get; set ;} [DataMember] public string dst {get; set ;}// Baidu translation returns the data structure // {// "from": "en", // "": "zh", // "trans_result": [// {// "src": "today", // "dst": "today "//}, // {// "src": "tomorrow", // "dst": "tomorrow" //}/], // "error_code": "52001 ", // "error_msg": "TIMEOUT", // "Query ": "he's" //} // <summary> // use the Post method to submit data /// </summary> /// <param name = "DatamarketAccessUri"> target URL </param> /// <param name = "requestDetails"> parameter string </param> /// <returns> </returns> private admaccesen en HttpPost (string DatamarketAccessUri, string requestDetails) {// Prepare OAuth request WebRequest webRequest = WebRequest. create (DatamarketAccessUri); webRequest. contentType = "applicati On/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 (DmAccessToken); // 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 = "Enter the content to translate" ;}} 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 = requested 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] p Ublic List <TokenResult> trans_result {get; set ;}} [DataContract] public class TokenResult {[DataMember] public string src {get; set ;} [DataMember] public string dst {get; set ;}// Baidu translation returns the data structure // {// "from": "en", // "": "zh", // "trans_result": [// {// "src": "today", // "dst": "today "//}, // {// "src": "tomorrow", // "dst": "tomorrow" //}/], // "error_code": "52001 ", // "error_msg": "TIMEOUT", // "Query ": "he's" //} // <summary> // use the Post method to submit data /// </summary> /// <param name = "DatamarketAccessUri"> target URL </param> /// <param name = "requestDetails"> parameter string </param> /// <returns> </returns> private admaccesen en HttpPost (string DatamarketAccessUri, string requestDetails) {// Prepare OAuth request WebRequest webRequest = WebRequest. create (DatamarketAccessUri); webRequest. contentType = "applicati On/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 (DmAccessToken); // 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 = "Enter the content to translate ";}}}