Ejemplo n.º 1
0
        private string errorInfo; //错误信息

        public string Translate(string sourceText, string desLang, string srcLang)
        {
            if (sourceText == "" || desLang == "" || srcLang == "")
            {
                errorInfo = "Param Missing";
                return(null);
            }

            if (desLang == "kr")
            {
                desLang = "kor";
            }
            if (srcLang == "kr")
            {
                srcLang = "kor";
            }
            if (desLang == "fr")
            {
                desLang = "fra";
            }
            if (srcLang == "fr")
            {
                srcLang = "fra";
            }

            // 原文
            string q = sourceText;

            string retString;

            Random rd   = new Random();
            string salt = rd.Next(100000).ToString();

            string sign = CommonFunction.EncryptString(appId + q + salt + secretKey);
            var    sb   = new StringBuilder("https://api.fanyi.baidu.com/api/trans/vip/translate?")
                          .Append("q=").Append(HttpUtility.UrlEncode(q))
                          .Append("&from=").Append(srcLang)
                          .Append("&to=").Append(desLang)
                          .Append("&appid=").Append(appId)
                          .Append("&salt=").Append(salt)
                          .Append("&sign=").Append(sign);
            string url = sb.ToString();

            var hc = CommonFunction.GetHttpClient();

            try
            {
                retString = hc.GetStringAsync(url).GetAwaiter().GetResult();
            }
            catch (System.Net.Http.HttpRequestException ex)
            {
                errorInfo = ex.Message;
                return(null);
            }
            catch (System.Threading.Tasks.TaskCanceledException ex)
            {
                errorInfo = ex.Message;
                return(null);
            }

            BaiduTransOutInfo oinfo = JsonConvert.DeserializeObject <BaiduTransOutInfo>(retString);

            if (oinfo.error_code == null || oinfo.error_code == "52000")
            {
                //得到翻译结果
                if (oinfo.trans_result.Count == 1)
                {
                    return(oinfo.trans_result[0].dst);
                }
                else
                {
                    errorInfo = "UnknownError";
                    return(null);
                }
            }
            else
            {
                errorInfo = "ErrorID:" + oinfo.error_code;
                return(null);
            }
        }
Ejemplo n.º 2
0
        private string errorInfo; //错误信息

        public async Task <string> TranslateAsync(string sourceText, string desLang, string srcLang)
        {
            if (sourceText == "" || desLang == "" || srcLang == "")
            {
                errorInfo = "Param Missing";
                return(null);
            }

            if (desLang == "kr")
            {
                desLang = "kor";
            }
            if (srcLang == "kr")
            {
                srcLang = "kor";
            }
            if (desLang == "fr")
            {
                desLang = "fra";
            }
            if (srcLang == "fr")
            {
                srcLang = "fra";
            }

            // 原文
            string q = sourceText;

            string retString;

            string salt = CommonFunction.RD.Next(100000).ToString();

            string sign = CommonFunction.EncryptString(appId + q + salt + secretKey);
            var    sb   = new StringBuilder("https://api.fanyi.baidu.com/api/trans/vip/translate?")
                          .Append("q=").Append(HttpUtility.UrlEncode(q))
                          .Append("&from=").Append(srcLang)
                          .Append("&to=").Append(desLang)
                          .Append("&appid=").Append(appId)
                          .Append("&salt=").Append(salt)
                          .Append("&sign=").Append(sign);
            string url = sb.ToString();

            var hc = CommonFunction.GetHttpClient();

            try
            {
                retString = await hc.GetStringAsync(url);
            }
            catch (System.Net.Http.HttpRequestException ex)
            {
                errorInfo = ex.Message;
                return(null);
            }
            catch (System.Threading.Tasks.TaskCanceledException ex)
            {
                errorInfo = ex.Message;
                return(null);
            }

            BaiduTransOutInfo oinfo = JsonConvert.DeserializeObject <BaiduTransOutInfo>(retString);

            if (oinfo.error_code == null || oinfo.error_code == "52000")
            {
                // 52000就是成功
                if (oinfo.trans_result.Length == 0)
                {
                    return("");
                }
                else if (oinfo.trans_result.Length == 1)
                {
                    return(oinfo.trans_result[0].dst);
                }
                else
                {
                    var sb2 = new StringBuilder();
                    foreach (var entry in oinfo.trans_result)
                    {
                        sb2.AppendLine(entry.dst);
                    }
                    return(sb2.ToString());
                }
            }
            else
            {
                errorInfo = "ErrorID:" + oinfo.error_code;
                return(null);
            }
        }
Ejemplo n.º 3
0
        private string errorInfo; //错误信息

        public string Translate(string sourceText, string desLang, string srcLang)
        {
            if (sourceText == "" || desLang == "" || srcLang == "")
            {
                errorInfo = "Param Missing";
                return(null);
            }

            if (desLang == "kr")
            {
                desLang = "kor";
            }
            if (srcLang == "kr")
            {
                srcLang = "kor";
            }
            if (desLang == "fr")
            {
                desLang = "fra";
            }
            if (srcLang == "fr")
            {
                srcLang = "fra";
            }

            // 原文
            string q = sourceText;

            string retString;

            Random rd   = new Random();
            string salt = rd.Next(100000).ToString();

            string sign = CommonFunction.EncryptString(appId + q + salt + secretKey);
            string url  = "http://api.fanyi.baidu.com/api/trans/vip/translate?";

            url += "q=" + HttpUtility.UrlEncode(q);
            url += "&from=" + srcLang;
            url += "&to=" + desLang;
            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;
            try
            {
                HttpWebResponse response         = (HttpWebResponse)request.GetResponse();
                Stream          myResponseStream = response.GetResponseStream();
                StreamReader    myStreamReader   = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
            }
            catch (WebException ex)
            {
                errorInfo = "Request Timeout";
                return(null);
            }

            BaiduTransOutInfo oinfo = JsonConvert.DeserializeObject <BaiduTransOutInfo>(retString);

            if (oinfo.error_code == null || oinfo.error_code == "52000")
            {
                //得到翻译结果
                if (oinfo.trans_result.Count == 1)
                {
                    return(oinfo.trans_result[0].dst);
                }
                else
                {
                    errorInfo = "UnknownError";
                    return(null);
                }
            }
            else
            {
                errorInfo = "ErrorID:" + oinfo.error_code;
                return(null);
            }
        }