|
|
@ -61,16 +61,18 @@ namespace STranslate.Utils
|
|
|
|
/// <returns></returns>
|
|
|
|
/// <returns></returns>
|
|
|
|
public static async Task<string> TranslateBaiduAsync(string appID, string secretKey, string text, LanguageEnum target, LanguageEnum source = LanguageEnum.AUTO)
|
|
|
|
public static async Task<string> TranslateBaiduAsync(string appID, string secretKey, string text, LanguageEnum target, LanguageEnum source = LanguageEnum.AUTO)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Random rd = new Random();
|
|
|
|
try
|
|
|
|
string salt = rd.Next(100000).ToString();
|
|
|
|
{
|
|
|
|
string sign = EncryptString(appID + text + salt + secretKey);
|
|
|
|
Random rd = new Random();
|
|
|
|
string url = "http://api.fanyi.baidu.com/api/trans/vip/translate?";
|
|
|
|
string salt = rd.Next(100000).ToString();
|
|
|
|
url += "q=" + HttpUtility.UrlEncode(text);
|
|
|
|
string sign = EncryptString(appID + text + salt + secretKey);
|
|
|
|
url += "&from=" + source.ToString().ToLower();
|
|
|
|
string url = "http://api.fanyi.baidu.com/api/trans/vip/translate?";
|
|
|
|
url += "&to=" + target.ToString().ToLower();
|
|
|
|
url += "q=" + HttpUtility.UrlEncode(text);
|
|
|
|
url += "&appid=" + appID;
|
|
|
|
url += "&from=" + source.ToString().ToLower();
|
|
|
|
url += "&salt=" + salt;
|
|
|
|
url += "&to=" + target.ToString().ToLower();
|
|
|
|
url += "&sign=" + sign;
|
|
|
|
url += "&appid=" + appID;
|
|
|
|
|
|
|
|
url += "&salt=" + salt;
|
|
|
|
|
|
|
|
url += "&sign=" + sign;
|
|
|
|
#if false
|
|
|
|
#if false
|
|
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
|
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
|
|
|
request.Method = "GET";
|
|
|
|
request.Method = "GET";
|
|
|
@ -85,13 +87,18 @@ namespace STranslate.Utils
|
|
|
|
myResponseStream.Close();
|
|
|
|
myResponseStream.Close();
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
var retString = await HttpUtil.GetAsync(url);
|
|
|
|
var retString = await HttpUtil.GetAsync(url);
|
|
|
|
var resp = JsonConvert.DeserializeObject<BaiduResp>(retString);
|
|
|
|
var resp = JsonConvert.DeserializeObject<BaiduResp>(retString);
|
|
|
|
if (resp.From != null)
|
|
|
|
if (resp.From != null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return resp.TransResult[0]?.Dst;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return string.Empty;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return resp.TransResult[0]?.Dst;
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return string.Empty;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 计算MD5值
|
|
|
|
// 计算MD5值
|
|
|
|
public static string EncryptString(string str)
|
|
|
|
public static string EncryptString(string str)
|
|
|
|