public BaiduTranslater()
        {
            try
            {
                string url = "https://fanyi.baidu.com/";

                BaiduWebRead = new WebApi.WebReader(@"fanyi.baidu.com");
                var tmpResult = BaiduWebRead.GetWebData(url, WebApi.WebReader.WebMethods.GET);

                tmpResult = BaiduWebRead.GetWebDataAndSetCookie(url, WebApi.WebReader.WebMethods.GET);
                Regex tokenRegex = new Regex("token: '(.*)'");
                Regex gtkRegex   = new Regex("gtk = '(.*)'");

                var tokenMatch = tokenRegex.Match(tmpResult);
                var gtkMatch   = gtkRegex.Match(tmpResult);

                if (tokenMatch.Success && gtkMatch.Success)
                {
                    token = tokenMatch.Value;
                    gtk   = gtkMatch.Value;
                    token = token.Substring(8, token.Length - 8).TrimEnd(new char[] { (char)39 });
                    gtk   = gtk.Substring(7, gtk.Length - 7).TrimEnd(new char[] { (char)39 });
                }
                string s = "In order to resolve this, Kindly go to the below path";
                Translate(s, "en", "zh");
            }
            catch (Exception e)
            {
                Logger.WriteLog(Convert.ToString(e));
            }
        }
Пример #2
0
        private void InitTranslator()
        {
            _IsInitialised        = false;
            _InitializationFailed = false;

            Task.Run(() =>
            {
                try
                {
                    _BaiduEncoder = new BaiduReqEncoder(GlobalTranslationSettings.BaiduEncoder, _Logger);

                    string url = "https://fanyi.baidu.com/";

                    var tmpResult = _BaiduWebRead.GetWebData(url, WebApi.WebReader.WebMethods.GET);

                    tmpResult        = _BaiduWebRead.GetWebDataAndSetCookie(url, WebApi.WebReader.WebMethods.GET);
                    Regex tokenRegex = new Regex("token: '(.*)'");
                    Regex gtkRegex   = new Regex("gtk = '(.*)'");

                    Match tokenMatch = tokenRegex.Match(tmpResult);
                    Match gtkMatch   = gtkRegex.Match(tmpResult);

                    if (tokenMatch.Success && gtkMatch.Success)
                    {
                        _Token = tokenMatch.Value;
                        _Gtk   = gtkMatch.Value;
                        _Token = _Token.Substring(8, _Token.Length - 8).TrimEnd(new char[] { (char)39 });
                        _Gtk   = _Gtk.Substring(7, _Gtk.Length - 7).TrimEnd(new char[] { (char)39 });

                        _IsInitialised = true;
                    }
                    else
                    {
                        _IsInitialised        = false;
                        _InitializationFailed = true;
                    }
                }
                catch (Exception e)
                {
                    _InitializationFailed = true;
                    _IsInitialised        = false;
                    _Logger?.WriteLog(e.ToString());
                }
            });
        }