This article mainly introduced the ASP.net call Baidu translation method, is aimed at Baidu interface development of the classic practical skills, very practical value, the need for friends can refer to the
-->
The example of this article tells the method that ASP.net calls Baidu translation. Share to everyone for your reference. The specific analysis is as follows:
asp.net call Baidu translation, as shown in the following image:
The HTML code is as follows:
The code is as follows: <%@ 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 ">
<title>oa translation </title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:textbox id= "Sourceword" runat= "columns=" rows= "style=" width:100%;
Textmode= "MultiLine" ></asp:TextBox>
<br/>
SOURCE language: <asp:dropdownlist id= "Ddlfrom" runat= "Server" >
<asp:listitem value= "Auto" > Automatic detection </asp:ListItem>
<asp:listitem value= "en" > Chinese </asp:ListItem>
<asp:listitem value= "en" > English </asp:ListItem>
<asp:listitem value= "JP" > Japanese </asp:ListItem>
</asp:DropDownList>
Target language: <asp:dropdownlist id= "Ddlto" runat= "Server" >
<asp:listitem value= "Auto" > Automatic detection </asp:ListItem>
<asp:listitem value= "en" > Chinese </asp:ListItem>
<asp:listitem value= "en" > English </asp:ListItem>
<asp:listitem value= "JP" > Japanese </asp:ListItem>
</asp:DropDownList>
<asp:button id= "Translate"
runat= "Server" text= "translation" onclick= "Translate_click"/>
<br/>
<asp:textbox id= "Resulttext" runat= "Server" textmode= "MultiLine" rows= "columns=" "style=" "width:100%"; ></asp:TextBox>
</div>
</form>
</body>
The C # code is as follows:
The code is as follows: 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 = "ID of the client_id= application";
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;}
}
Baidu Translation return data structure
//{
"From": "en",
"To": "en",
"Trans_result": [
// {
"src": "Today",
"DST": "Today"
// },
// {
"src": "Tomorrow",
"DST": "Tomorrow"
// }
//],
"Error_code": "52001",
"Error_msg": "TIMEOUT",
"Query": "He ' s"
//}
<summary>
Submit data by post
</summary>
<param name= "Datamarketaccessuri" > Target URL </param>
<param name= "requestdetails" > Parameter string </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 = "Please enter the content to be translated";
}
}
}
I hope this article will help you with the C # program.