Exemplo n.º 1
0
            private static readonly string key   = "2gH17Xp3c1L4jkBp8bQl";                                     // 密钥

            /// <summary>
            /// 百度翻译
            /// </summary>
            /// <param name="text">要翻译的文本</param>
            /// <param name="from">源语言 例如 en 更多简写参考 http://api.fanyi.baidu.com/api/trans/product/apidoc </param>
            /// <param name="to">目标语言 例如 zh</param>
            /// <returns></returns>
            public static string Translate(string text, string from, string to)
            {
                string result = text;

                if (string.IsNullOrWhiteSpace(text) || string.IsNullOrWhiteSpace(from) || string.IsNullOrWhiteSpace(to))
                {
                    return(result);
                }
                string body = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(text));
                string salt = "1";                                                                     // 随机数
                string sign = MD5Util.Md5(string.Concat(appid, body, salt, key));                      // 签名

                body = HttpUtility.UrlEncode(body);                                                    // 生成签名之后,发送HTTP请求之前
                string        url           = string.Format("http://api.fanyi.baidu.com/api/trans/vip/translate?q={0}&from={1}&to={2}&appid={3}&salt={4}&sign={5}", body, from.ToLower(), to.ToLower(), appid, salt, sign.ToLower());
                ReqeustResult reqeustResult = HttpRequestHelper.Request(url, new RequestData()
                {
                    Method = RequestMethods.Get
                });
                //根据API返回的Json格式 构建匿名对象
                var obj = new
                {
                    from         = "",
                    to           = "",
                    trans_result =
                        new[]
                    {
                        new
                        {
                            src = "",
                            dst = ""
                        }
                    }
                };
                var baiduTransResult = JsonConvert.DeserializeAnonymousType(reqeustResult.Html, obj);

                //判断是否正确的返回翻译结果
                if (baiduTransResult.trans_result != null)
                {
                    result = baiduTransResult.trans_result[0].dst;
                }
                return(result);
            }
Exemplo n.º 2
0
        /// <summary>
        /// 短URL
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string[] ShortUrl(string url)
        {
            //可以自定义生成MD5加密字符传前的混合KEY
            //要使用生成URL的字符
            string[] chars = new string[] {
                "a", "b", "c", "d", "e", "f", "g", "h",
                "i", "j", "k", "l", "m", "n", "o", "p",
                "q", "r", "s", "t", "u", "v", "w", "x",
                "y", "z", "0", "1", "2", "3", "4", "5",
                "6", "7", "8", "9", "A", "B", "C", "D",
                "E", "F", "G", "H", "I", "J", "K", "L",
                "M", "N", "O", "P", "Q", "R", "S", "T",
                "U", "V", "W", "X", "Y", "Z"
            };

            //对传入网址进行MD5加密
            string hex = MD5Util.Get(url);

            string[] strings = new string[4];
            for (int i = 0; i < 4; i++)
            {
                //把加密字符按照8位一组16进制与0x3FFFFFFF进行位与运算
                int    hexint   = 0x3FFFFFFF & Convert.ToInt32("0x" + hex.Substring(i * 8, 8), 16);
                string outChars = string.Empty;
                for (int j = 0; j < 6; j++)
                {
                    //把得到的值与0x0000003D进行位与运算,取得字符数组chars索引
                    int index = 0x0000003D & hexint;
                    //把取得的字符相加
                    outChars += chars[index];
                    //每次循环按位右移5位
                    hexint = hexint >> 5;
                }
                //把字符串存入对应索引的输出数组
                strings[i] = outChars;
            }
            return(strings);
        }