コード例 #1
0
        public async Task <string> TranslateAsync(string sourceText, string desLang, string srcLang)
        {
            if (sourceText == "" || desLang == "" || srcLang == "")
            {
                errorInfo = "Param Missing";
                return(null);
            }

            // 原文
            string q = HttpUtility.UrlEncode(sourceText);

            string retString;

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

            string url = "https://tmt.tencentcloudapi.com/?";

            // 签名,参数使用未URL编码的值
            var sb = new StringBuilder()
                     .Append("Action=TextTranslate")
                     .Append("&Nonce=").Append(salt)
                     .Append("&ProjectId=0")
                     .Append("&Region=ap-shanghai")
                     .Append("&SecretId=").Append(SecretId)
                     .Append("&Source=").Append(srcLang)
                     .Append("&SourceText=").Append(sourceText)
                     .Append("&Target=").Append(desLang)
                     .Append("&Timestamp=").Append(CommonFunction.GetTimeStamp())
                     .Append("&Version=2018-03-21");
            string req = sb.ToString();

            HMACSHA1 hmac = new HMACSHA1()
            {
                Key = Encoding.UTF8.GetBytes(SecretKey)
            };

            byte[] data   = Encoding.UTF8.GetBytes("GETtmt.tencentcloudapi.com/?" + req);
            var    result = hmac.ComputeHash(data);

            hmac.Dispose();

            // 请求参数,参数使用URL编码后的值
            sb = new StringBuilder()
                 .Append("Action=TextTranslate")
                 .Append("&Nonce=").Append(salt)
                 .Append("&ProjectId=0")
                 .Append("&Region=ap-shanghai")
                 .Append("&SecretId=").Append(SecretId)
                 .Append("&Source=").Append(srcLang)
                 .Append("&SourceText=").Append(q)
                 .Append("&Target=").Append(desLang)
                 .Append("&Timestamp=").Append(CommonFunction.GetTimeStamp())
                 .Append("&Version=2018-03-21")
                 .Append("&Signature=").Append(HttpUtility.UrlEncode(Convert.ToBase64String(result)));
            req = sb.ToString();

            var hc = CommonFunction.GetHttpClient();

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

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

            if (oinfo.Response.Error == null)
            {
                //得到翻译结果
                return(oinfo.Response.TargetText);
            }
            else
            {
                errorInfo = "ErrorID:" + oinfo.Response.Error.Value.Code + " ErrorInfo:" + oinfo.Response.Error.Value.Message;
                return(null);
            }
        }
コード例 #2
0
        public string Translate(string sourceText, string desLang, string srcLang)
        {
            if (sourceText == "" || desLang == "" || srcLang == "")
            {
                errorInfo = "Param Missing";
                return(null);
            }

            // 原文
            string q = sourceText;

            string retString;

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

            string url = "https://tmt.tencentcloudapi.com/?";

            string req = "Action=TextTranslate";

            req += "&Nonce=" + salt;
            req += "&ProjectId=0";
            req += "&Region=ap-shanghai";
            req += "&SecretId=" + SecretId;
            req += "&Source=" + srcLang;
            req += "&SourceText=" + sourceText;
            req += "&Target=" + desLang;
            req += "&Timestamp=" + CommonFunction.GetTimeStamp();
            req += "&Version=2018-03-21";

            HMACSHA1 hmac = new HMACSHA1()
            {
                Key = System.Text.Encoding.UTF8.GetBytes(SecretKey)
            };

            byte[] data   = Encoding.UTF8.GetBytes("GETtmt.tencentcloudapi.com/?" + req);
            var    result = hmac.ComputeHash(data);

            req = req + "&Signature=" + HttpUtility.UrlEncode(Convert.ToBase64String(result));

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + req);

            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);
            }

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

            if (oinfo.Response.Error == null)
            {
                //得到翻译结果
                return(oinfo.Response.TargetText);
            }
            else
            {
                errorInfo = "ErrorID:" + oinfo.Response.Error.Code + " ErrorInfo:" + oinfo.Response.Error.Message;
                return(null);
            }
        }