1,基本使用
1.1 百度翻译开发平台 (注册,登录)http://api.fanyi.baidu.com/ 获取 appid 和 secretKey 2.2 代码 一 以控制台为例 (可以下载例子查看unity和C#完整代码) demo地址
public static void SpeakEn(string q)
{
try
{
string from = "zh";
string to = "en";
string appId = "201911060003536890";
Random rd = new Random();
string salt = rd.Next(100000).ToString();
string secretKey = "I5vLXdWR_PahbFBSDj9F1";
string sign = EncryptString(appId + q + salt + secretKey);
string url = "http://api.fanyi.baidu.com/api/trans/vip/translate?";
url += "q=" + HttpUtility.UrlEncode(q);
url += "&from=" + from;
url += "&to=" + to;
url += "&appid=" + appId;
url += "&salt=" + salt;
url += "&sign=" + sign;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
request.UserAgent = null;
request.Timeout = 6000;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
Console.WriteLine("接受信息:" + retString);
}
catch (Exception e)
{
Console.WriteLine("错误:" + e.Message);
}
}
2,提供下载地址
下载地址 demo地址
|