Exemple #1
0
        public virtual string GetContent(string word, string codeForm, string codeTo)
        {
            m_ExceptionMessage = "";
            if (string.IsNullOrEmpty(word))
            {
                return(string.Empty);
            }

            this.ResetLastResultFromResponse();
            LangPair lp = new LangPair(codeForm, codeTo);
            // TODO: некрасивый дизайн
            string _acceptedLanguageCode = GetContainsSourceLanguage(lp.ToString());

            if (string.IsNullOrEmpty(_acceptedLanguageCode)) // проверка поддержки языка
            {
                return("");                                  // язык не поддерживается
            }
            string url = this.GetUrl(word, lp);


            //TODO: если пользователь меняет конфигурацию словарей то надо сбросить
            //TODO: историю для текстов храним?? может по ключу текста определятся))
            if (history.ContainsKey(word + _acceptedLanguageCode))
            {
                m_LastResultFromResponse = new ResultFromResponse(url, _acceptedLanguageCode, history[word + _acceptedLanguageCode]);
                return(history[word + _acceptedLanguageCode]);
            }

            string response = "";

            if (IsDoFullLoading)
            {
                response = DoAddLoading(url);
            }
            else
            {
                #region Getting response
                #region request
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                request.Proxy     = null;
                request.UserAgent = "Mozilla/5"; // "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 GTB7.1 ( .NET CLR 3.5.30729; .NET4.0E) GTBA"

                /////////////////////////////////////////
                // headers test for www.euronews.com
//request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1920.0 Safari/537.36";
//request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
//request.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
//request.Headers.Add("Accept-Language", "ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4");
//request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.Default); // "Cache-Control", "max-age=0";
//request.Headers.Add("Cache-Control", "max-age=0");

//request.KeepAlive = true; //request.Connection = "Keep-alive";

////string raw = "__gads=ID=63ac347ea76af89f:T=1395648872:S=ALNI_MaprzlJGOpP_i-K7nBBgJlgGyBpyg; EN_meteo_fc=C; EN_textSize=1; __atuvc=5%7C13%2C3%7C14";
////string rawAr = raw.Split('=', ';');

//Uri target = new Uri("http://euronews.com/");
//request.CookieContainer = new CookieContainer();
//request.CookieContainer.Add(new Cookie("__gads", "ID=63ac347ea76af89f:T=1395648872:S=ALNI_MaprzlJGOpP_i-K7nBBgJlgGyBpyg")  { Domain = target.Host });
//request.CookieContainer.Add(new Cookie("EN_meteo_fc", "C") { Domain = target.Host });
//request.CookieContainer.Add(new Cookie("EN_textSize", "1") { Domain = target.Host });
//request.CookieContainer.Add(new Cookie("__atuvc", "5%7C13%2C3%7C14") { Domain = target.Host });
//request.Host = "euronews.com";
                /////////////////////////////////////////

                //  request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                //  request.Headers.Add("Accept-Language", "en-us,en;q=0.5");
                //  request.Headers.Add("Accept-Encoding", "gzip,deflate");
                //  request.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
                //  request.Headers.Add("Cookie", "PREF=ID=e0d94a24a2fa9cc4:U=1c449fa392bc9632:TB=2:TM=1267174677:LM=1273649640:DV=8atQZBunt08B:GM=1:S=x8X2uigJVBwIPI8s; NID=34=B0FfFFFonIaztkSsAmNGRUc2d_zPSqDjWdTKuaQgtMO4v7jmGspcZB0SyUq8sjyCSzzdPeB8T9YeCoUXAn1gkqNecV5TheVtz7gBdH7QHCAU8r6g6H1xV4sNqo9oe-UT; rememberme=true; SID=DQAAAKUAAADbnRqRs2S1NuzS-DqVUTlfDNQUAGZmdptrFk_TtFGpcndN1-wH_J04MBexODkaLHgoJItKux_SIuL4xdCmYZ4PkG_hMSGA9I1u9aiIDP0BVejn5_rXhP6tLrK20SXoFrs2icfy-M3B-nwmhcQpzrSKAC9joJu3E36zvgbrokwZhHMr2oe7Jb2GzfvRb5dXilY0uw67CVTBS8vQtLw1qLPoKu99bi3lZGuXv7LSbTWn-g; HSID=Ap1PAdWAwDLJcgF58; S=indic-transliteration=eE3u86HcynSHRqj7jy_xdg");

                // for multitran
                //request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";
                //request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                //request.Headers.Add("Accept-Language", "en-us,en;q=0.5");
                //request.Headers.Add("Accept-Encoding", "gzip,deflate");
                //request.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
                //request.Headers.Add("Keep-Alive", "	115");
                //request.Headers.Add("Cookie", "	langs=2 1");
                #endregion

                #region Gets a Stream  object to use to write request data
                if (this.RequestParameters.Length > 0 &&
                    !this.URL.Contains("{0}")) // && this.StartTag == string.Empty)
                {
                    request.Method = "POST";
                    Stream newStream = request.GetRequestStream();


                    Encoding encoding = new ASCIIEncoding();
                    //  string encodingName = "koi8-r windows-1251";
                    //  string encodingName = Encoding.UTF8;
                    string encodingName = "ISO-8859-1";

                    encoding = System.Text.Encoding.GetEncoding(encodingName,
                                                                new EncoderReplacementFallback("(unknown)"),
                                                                new DecoderReplacementFallback("(error)"));
                    encoding = Encoding.Default;

                    //  Console.WriteLine("Request with EncodingName: " + encoding.EncodingName + " CodePage: " + encoding.CodePage);

                    byte[] byte1;

                    //byte1 = encoding.GetBytes("security_token=" + "AKrFfvIEjIbGcYBjuELVb-iUTz8SO_5xQg:1285153424972");
                    //newStream.Write(byte1, 0, byte1.Length);

                    //byte1 = encoding.GetBytes("entity=" + "00001otndccsdmo");
                    //newStream.Write(byte1, 0, byte1.Length);

                    //byte1 = encoding.GetBytes("esm=" + "true");
                    //newStream.Write(byte1, 0, byte1.Length);

                    //Console.WriteLine("!!!!!!!!");

                    byte1 = encoding.GetBytes(this.RequestParameters[0] + word);
                    newStream.Write(byte1, 0, byte1.Length);

                    //byte1 = encoding.GetBytes("sl=" + lp.From);
                    //newStream.Write(byte1, 0, byte1.Length);

                    //byte1 = encoding.GetBytes("tl=" + lp.To);
                    //newStream.Write(byte1, 0, byte1.Length);

                    newStream.Close();
                }
                #endregion

                #region GetResponse
                WebResponse resp = null;
                try
                {
                    resp = request.GetResponse();
                    //Console.WriteLine("Log: Get response for URL: '{0}'", url);
                }
                catch (WebException ex)
                {
                    // тем самым состояние установим что у нас были проблемы
                    m_ExceptionMessage = ex.Message + Environment.NewLine + request.Address.ToString();

                    if (this.IsOnGetResponseErrorThrowing)
                    {
                        throw ex;
                    }

                    if (!WWW.IsOnline())
                    {
                        m_ExceptionMessage = WWW.InternetIsUnavailable;
                        return(WWW.InternetIsUnavailable);
                    }

                    //var statusCode = ((HttpWebResponse)ex.Response).StatusCode;
                    //return "An error occurred, status code: " + statusCode;

                    // if (ex.Message.Contains("(404)"))
                    return(m_ExceptionMessage); // ex.Status == ProtocolError // from System.Net.WebExceptionStatus
                    //  if (ex.Status == WebExceptionStatus.NameResolutionFailure)
                    //Console.WriteLine("Errror: {0}, Response: {1}, URL: {2}", ex.Message, ex.Response, url);
                }
                #endregion

                if (resp == null)
                {
                    if (!WWW.IsOnline())
                    {
                        return(WWW.InternetIsUnavailable);
                    }
                    return("An error occurred while processing the request.");
                }
                response = GetStringFromResponse(resp);
                #endregion
            }

            this.LastRawResponse = response;

            if (this.StartTags != null && !string.IsNullOrEmpty(this.StartTags[0]))
            {
                response = GetContentFromTag(response);
            }

            // (c использованием base не получилось)
            //response = string.Format(@"<base target=""_blank"" /> <base href=""{0}"" /> ", this.CorrectionURL) + response;

            response = DoCorrectionForUrl(response, "href=\"#", url);               // for bookmarks
            response = DoCorrectionForUrl(response, "href=\"", this.CorrectionURL); // for href=" (c использованием base не получилось)
            response = DoCorrectionForUrl(response, "src=\"", this.CorrectionURLForImage);
            response = DoCorrectionForUrl(response, "href=/", this.CorrectionURL);  // for href=/ (c использованием base не получилось)
            response = DoCorrectionForUrl(response, "src=/", this.CorrectionURLForImage);
            response = DoCorrectionForUrl(response, "src='", this.CorrectionURLForImage);

            if (this.ClearFromScript)
            {
                response = DoCorrectionForScript(response);
            }

            if (this.ClearFromIframe)
            {
                response = DoCorrectionForIframe(response);
            }

            // for preventing opening new window (meat-in urban dictionary; unknownword - in wordReference)
            //response = response.Replace("<iframe", @"<base target=""_parent"" /> <iframe");
            //response = response.Replace("<iframe", @"<iframe target=""_parent""");

            lock (history)
            {
                if (!history.ContainsKey(word + _acceptedLanguageCode)) // другой поток мог успеть добавить
                {
                    history.Add(word + _acceptedLanguageCode, response);
                }
            }
            m_LastResultFromResponse = new ResultFromResponse(url, _acceptedLanguageCode, response);
            if (!String.IsNullOrEmpty(this.Styles))
            {
                string htmlTemplate = @"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN""><html><head>{0}</head><body>{1}</body></html>";
                response = string.Format(htmlTemplate, this.Styles, response);
            }
            return(response);
        }
Exemple #2
0
        //            "<table><tr><td class=\"topForArticle\"><a href=\"{0}\" title=\"{1}\">{2} ({3})</a></td></tr></table>";

        /*
         * .topForArticle
         * {
         *      background-color: #C0C0C0; padding-right: 50px; padding-left: 50px;
         * }
         *
         * static readonly string TopForArticleTemplate = "{0}\r\n<table><tr>{0}</tr></table>\r\n";
         */
        #endregion

        public static void Dict_Click(object sender, EventArgs e)
        {
            RunDictContent dictContent = ((ToolStripItem)sender).Tag as RunDictContent;

            if (dictContent == null)
            {
                return;
            }
            string word = dictContent.TextWithSelection.CurrentLowerWord;

            if (string.IsNullOrEmpty(word))
            {
                string message = "A word is not specified";
                // if (Application.ProductName  == DictionaryBlend message += " (Drag-n-drop supported)." // "You must type a word
                MessageBox.Show(message, Application.ProductName,
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                LangPair lp = dictContent.TextWithSelection.LangDir;
                if (dictContent.UITarget != null)
                {
                    #region for async calling
                    WaitingUIObjectWithFinish waitObject = null;
                    //if ((dictContent.Providers.Count == 1) && !(dictContent.Providers[0] is FavoritProvider))
                    if (dictContent.TextWithSelection is ITextWithSelectionAndWaiting)
                    {
                        MethodInvoker onFinish = new MethodInvoker(AssignTextToControl);
                        waitObject = new WaitingUIObjectWithFinish(((ITextWithSelectionAndWaiting)dictContent.TextWithSelection).Control,
                                                                   ((ITextWithSelectionAndWaiting)dictContent.TextWithSelection).Picture, onFinish);
                        lastUITarget = dictContent.UITarget;
                        foreach (AsyncProvider provider in currentRequests)
                        {
                            if (provider.CurrentThread.IsAlive)
                            {
                                provider.CurrentThread.Abort();
                            }
                        }
                        currentRequests.Clear();
                        lock (resultContainer)
                            resultContainer.Clear();
                    }
                    #endregion

                    using (new WaitCursor(waitObject == null))
                    {
                        // System.Threading.Thread.Sleep(3000);

                        string dictBodies = "";
                        foreach (DictionaryProvider provider in dictContent.Providers)
                        {
                            //if (provider is FavoritProvider)
                            //{
                            //    new Gator.GatorStarter(word, lp.From, lp.To, null); // waitingUIObject
                            //    dictBodies = "See result in an external window";
                            //}
                            //else
                            {
                                if (!provider.IsSupport(lp.ToString()))
                                {
                                    continue;
                                }
                                string url           = provider.GetPublicUrl(word, lp);
                                string providerTitle = provider.Title;
                                if (provider.GetType().Name.ToLower().Contains("google"))
                                {
                                    providerTitle = "Google " + providerTitle;
                                }
                                string title = string.Format(topForArticleTemplate, url, providerTitle, provider.Copyright);
                                if (waitObject != null)
                                {
                                    currentRequests.Add(new AsyncProvider(word, lp, provider, waitObject, resultContainer, title));
                                }
                                else
                                {
                                    dictBodies += title;
                                    string content = provider.GetContent(word, lp);
                                    dictBodies += content;
                                }
                            }
                        }
                        if (waitObject == null)
                        {
                            dictContent.UITarget.TempContent = dictBodies;
                        }
                        dictContent.UITarget.AssignText(dictBodies);
                    }
                }
                else // here just working with urls and external browser
                {
                    #region Runner.OpenURL
                    foreach (DictionaryProvider provider in dictContent.Providers)
                    {
                        if (provider is FavoritProvider)
                        {
                            new Gator.GatorStarter(word, lp.From, lp.To, null); // waitingUIObject
                        }
                        else
                        {
                            if (!provider.IsSupport(lp.ToString()))
                            {
                                continue;
                            }
                            Runner.OpenURL(provider.GetPublicUrl(word, lp));
                        }
                    }
                    #endregion
                }
            }
        }