Ejemplo n.º 1
0
        public static async Task <BaiduTranslatorResultDto> TranslatorPost(string content, string from = null, string to = null)
        {
            // 判断翻译语言
            if (from != null)
            {
                From = from;
            }
            if (to != null)
            {
                To = to;
            }
            // 签名
            Sign = MD5Service.GenerateMD5(APPID + content + Salt + SecretKey);
            // 参数
            var parameter = string.Format(Parameter, HttpUtility.UrlEncode(content), From, To, APPID, Salt, Sign);
            // GetAsync请求
            // 如使用POST方式,Content-Type请指定为:application/x-www-form-urlencoded
            // PostAsync请求(由于Get请求可能出现Url过长,获取不到数据,所以采用Post请求)
            var resultString = await HttpRequestService.HttpPostAsync(Url, parameter, "application/x-www-form-urlencoded");

            // 去除转义字符
            //foreach (char c in resultString)
            //{
            //    if (c != '\\') resultStringBuilder.Append(c);
            //}
            // 返回字符串转换为JSON对象
            return(JsonConvert.DeserializeObject <BaiduTranslatorResultDto>(resultString));
        }