Asp. NET calls Baidu Translation

Source: Internet
Author: User
Tags oauth


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

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.