Daily API Translation: Baidu translation and api Translation
What is Baidu translation? Can I eat it? I believe many people are familiar with it. It is an essential part of our life.
However, the Baidu translation development platform can only translate 2 million characters each month, and the additional characters are calculated as 49.00/million characters. For the beggar programmers who make me soy sauce, it is actually enough.
Next, let's start with the question. There are two parts: one is free Baidu translation, and the other is paid Baidu translation (no one will check it here)
First, let's talk about the free API. The request is simple and fast.
I. Free Version (unrestricted and fast)
Open a Cat first. No, It is translation and Chrome (as long as the browser can capture packets, Chrome is used as an example here)
We can find a request named v2transapi, from which we can see that it is sent in POST mode.
Get API:
Http://fanyi.baidu.com/v2transapi
Use C # For sample code:
1 public static async Task<string> PostWebAsync(string url, string idata) 2 { 3 var request = (HttpWebRequest)WebRequest.Create(url); 4 var data = Encoding.ASCII.GetBytes(idata); 5 request.Method = "POST"; 6 request.ContentType = "application/x-www-form-urlencoded"; 7 request.ContentLength = data.Length; 8 using (var stream = await request.GetRequestStreamAsync()) 9 {10 await stream.WriteAsync(data, 0, data.Length);11 }12 13 var response = (HttpWebResponse)await request.GetResponseAsync();14 15 var r = new StreamReader(response.GetResponseStream());16 // System.Windows.MessageBox.Show(await r.ReadToEndAsync());17 return await r.ReadToEndAsync();18 }
We need a method to send a POST request, and then use:
1 string data = PostWebAsync ("http://fanyi.baidu.com/v2transapi ", "from = auto & to = {language after translation} & query = {translated content} & transtype = realtime & simple_means_flag = 3 ");
For the from and to attributes, refer to Baidu translation API code.
After obtaining the data, we need to parse it.
The translated text is in the position of ["trans_result"] ["data"] [0] ["dst"] in JSON.
Ii. Paid version (Limited, fast, and difficult to develop)
First, you need to register a Baidu account to apply for the service at api.fanyi.baidu.com.
The signature generation method is as follows:
1. Translate the APPID (appid) in the Request Parameters, query (q, attention to UTF-8 encoding), random number (salt), and platform-assigned keys (which can be viewed on the console)
String 1 is obtained by splicing appid + q + salt + key.
2. Perform md5 on string 1 to obtain the 32-bit lowercase sign.
Provide C # MD5 encryption:
1 public class MD5 2 { 3 public static byte[] EncryptToMD5(string str) 4 { 5 MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); 6 byte[] str1 = System.Text.Encoding.UTF8.GetBytes(str); 7 byte[] str2 = md5.ComputeHash(str1, 0, str1.Length); 8 md5.Clear(); 9 (md5 as IDisposable).Dispose();10 return str2;11 }12 public static string EncryptToMD5string(string str)13 {14 byte[] bytHash = EncryptToMD5(str);15 string sTemp = "";16 for (int i = 0; i < bytHash.Length; i++)17 {18 sTemp += bytHash[i].ToString("X").PadLeft(2, '0');19 }20 return sTemp.ToLower();21 }22 }
Request address:
"Http://api.fanyi.baidu.com/api/trans/vip/translate? Q = "+ q +" & from = "+ from +" & to = "+ to +" & appid = 20151231000008489 & salt = 2004112629 "+" & sign = "+ sign
Note:
1. Convert the text to be translated into UTF-8 encoding first
2. perform URL encode for each field before sending an HTTP request.
3. When generating a signature and splicing an appid + q + salt + key string, q does not need to be a URL encode. After the signature is generated, before sending an HTTP request, you must perform URL encode on the q field to be translated.
Finally, a JSON file is obtained. The correct translation result is located in ["trans_result"] [0] ["dst"] And the UFT8 decoding process is complete.
Ah meow, this tutorial is over now. Goodbye next time!