Case using Baidu translation API interface to achieve text translation
To ensure the quality of the translation, please control the single request length within 6000 bytes. (about 2000 Chinese characters)
The signature generation method is as follows:
1. APPID (APPID) in the request parameter, translate query (q, note for UTF-8 encoding), random number (salt), and platform-assigned key (viewable in the admin console)
The string 1 is stitched in the order of the appid+q+salt+ key.
2, the string 1 do md5, get 32 lowercase sign.
Attention:
1. Please convert the text you need to translate to UTF-8 code first
2. You need to make the URL encode for each field before sending the HTTP request.
3, when generating the signature stitching appid+q+salt+ key string, Q does not need to do the URL encode, after the signature is generated, before sending an HTTP request to the text field to be sent to the Q URL encode.
Translate apple from English to Chinese:
Request Parameters:
Q=apple
from=en
To=zh
appid=2015063000000001
salt=1435660288
Platform-assigned key: 12345678
Generate sign:
> Stitching Strings 1
Stitching appid=2015063000000001+q=apple+salt=1435660288+ key =12345678
Get string 1 =2015063000000001Apple143566028812345678
> Calculate signature sign (do MD5 encryption on string 1, note that string 1 must be UTF-8 encoded before MD5 is calculated)
SIGN=MD5 (2015063000000001apple143566028812345678)
Sign=f89f9594663708c1605f3d736d01d2d4
The complete request is:
http://api.fanyi.baidu.com/api/trans/vip/translate?q=apple&from=en&to=zh&appid=2015063000000001 &salt=1435660288&sign=f89f9594663708c1605f3d736d01d2d4
You can also use the Post method to transfer the required parameters.
Public stringBaidufanyi () {stringQ ="Apple"; stringAppId ="159357147258369"; stringpassword="1s5d8d6g5f8cd4d5"; //Target Language stringto ="en"; //Source Language string from="ZH"; //get random numbers stringRandomnum =System.DateTime.Now.Millisecond.ToString (); //get a string that needs to be encrypted stringIndex =appid+q+randomnum+password; //to encrypt stringMd5sign =GetMD5 (index); //Create a connection address stringURL =string. Format ("http://api.fanyi.baidu.com/api/trans/vip/translate?q={0}&from={1}&to={2}&appid={3}&salt={4 }&SIGN={5}", Q, from, to,appid,randomnum,md5sign); WebClient WC=NewWebClient (); stringFanyi=WC. downloadstring (URL); returnFanyi; } /// <summary> ///MD5 Encryption/// </summary> /// <param name= "Input" >the text to encrypt</param> /// <returns></returns> Public stringGetMD5 (stringinput) { //determines whether the empty if(Input = =NULL) { return NULL; } //Create a default instance of the MD5 hash tableMD5 MD =MD5. Create (); //Convert the string to be encrypted to a byte array byte[] buffer =Encoding.UTF8.GetBytes (input); //computes the hash value of the specified byte array byte[] data =md.computehash (buffer); StringBuilder SB=NewStringBuilder (); //find yellow format each byte of the data in the HA to be 16 binary strings for(intI=0; I<data. length;i++) {sb. Append (Data[i]. ToString ("X2")); } //returns a 16 binary string returnsb. ToString (); }
Above, AppID and password are purely fictitious,
Baidu Translation API Interface implementation