What is Baidu Translation? Can I eat it? I believe many people are familiar with it. It is an indispensable thing in our life.
However, Baidu Translation Development Platform can only translate 2 million characters per month, and the extra characters should be calculated according to 49.00/million characters. For my purple beggar programmer, in fact, is enough.
Next comes the topic, which is divided into two parts: free Baidu translation and paid Baidu translation.
First, let's talk about free. This API is very simple and fast.
Free edition (unlimited and fast)
Open a Cat first, ah no, translation and Chrome (as long as the browser can grab the package, here use Chrome as an example)
You can find a request named v2transapi from which you can see that it was sent in POST mode.
Get API:
http://fanyi.baidu.com/v2transapi
Let's use C # as a code example:
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 way to send POST requests, and then use:
1 string data = PostWebAsync("http://fanyi.baidu.com/v2transapi","from=auto&to={Translated Language}&query={Contents of Translation}&transtype=realtime&simple_means_flag=3");
The from and to attributes can be filled in by referring to the code of Baidu Translation API.
After we get the data, we need to parse it.
The translated text is located in JSON: ["trans_result"]["data"][0]["dst"].
2. Charge Edition (Limited, Fast and Difficult to Develop)
Well, first you need to register for a Baidu account and apply for service at api.fanyi.baidu.com.
The signature generation method is as follows:
1. Translate the APPID(appid) in the request parameters, query(q, note UTF-8 encoding), random number (salt), and the key allocated by the platform (available in the administrative console)
String 1 is obtained by splicing the sequence of appid+q+salt+key.
2. Make md5 for string 1 and get 32-bit lowercase sign.
Enclosed is a C# MD5 encrypted piece:
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
Be careful:
1. Please first convert the translated text to UTF-8 encoding.
2. Before sending HTTP requests, you need to encode the URLs for each field.
3. When generating signature splicing appid+q+salt+key string, Q does not need to do URL encode. After generating signature, it needs to do URL encode for the text field Q to be translated before sending HTTP request.
Finally, a JSON is obtained, and the correctly translated content is located in ["trans_result"][0]["dst"] and the UFT8 decoding process can be carried out.
Ah Miao, this course is over here. See you next time!