Example #1
0
        private void StartWebRequest(string _searchString, int _index, int _totalReqNum, Action <ISearchResult> _OnResultReady)
        {
            //Debug.WriteLine(string.Format("StartWebRequest: {0}", _index));

            var tmpBaseSearchSuffix = string.Format(baseSearchStringSuffix, pageMultiplier * _index);
            //tmpResult = GetSearchResults(_searchString + tmpBaseSearchSuffix);

            HttpWebRequest    webRequest    = InitWebRequest(_searchString + tmpBaseSearchSuffix);
            HttpWebRequestExt webRequestExt = new HttpWebRequestExt(webRequest, _index, _totalReqNum, _OnResultReady);

            webRequest.BeginGetResponse(new AsyncCallback(FinishWebRequest), webRequestExt);
        }
Example #2
0
        private void FinishWebRequest(IAsyncResult result)
        {
            HttpWebRequestExt webRequestExt = (result.AsyncState as HttpWebRequestExt);
            HttpWebResponse   response      = webRequestExt.HttpWebRequest.EndGetResponse(result) as HttpWebResponse;

            ISearchResult searchResult = new SearchResult(searchEngine);

            searchResult.SearchResponseRaw = new StreamReader(response.GetResponseStream()).ReadToEnd();
            searchResult.RequestNum        = webRequestExt.RequestNum;

            lock (finishLocker) {
                lSearchResultsCache.Add(searchResult);
                //Debug.WriteLine(string.Format("lSearchResultsCache Count: {0} after Added result: {1}", lSearchResultsCache.Count, searchResult.SearchResponseRaw));
                if (lSearchResultsCache.Count == webRequestExt.TotalRequestNum)
                {
                    if (webRequestExt.OnResultReady != null)
                    {
                        ISearchResult totalResult   = new SearchResult(searchEngine);
                        var           sortedResults = lSearchResultsCache.OrderBy(r => r.RequestNum);
                        foreach (var item in sortedResults)
                        {
                            totalResult.SearchResponseRaw += item.SearchResponseRaw;
                        }

                        webRequestExt.OnResultReady(totalResult);
                    }
                    else
                    {
                        Debug.WriteLine("BaseSearch.FinishWebRequest: Error! Please assign OnResultReady");
                    }
                }
            }


            //lock (lSearchResultsCache) {
            //    // if list contains more elements than the Req.Num than we Insert otherwise Add
            //    if (lSearchResultsCache.Count > webRequestExt.RequestNum) {
            //        lSearchResultsCache.Insert(webRequestExt.RequestNum - 1, searchResult);
            //    } else {
            //        lSearchResultsCache.Add(searchResult);
            //    }
            //    // here we synch and check if the total request number equals already returned. If YES we raise OnresultReady event
            //    if (lSearchResultsCache.Count == webRequestExt.TotalRequestNum && webRequestExt.OnResultReady != null) {
            //        //compose total response
            //        ISearchResult totalResult = new SearchResult(searchEngine) { SearchString = webRequestExt.HttpWebRequest.RequestUri.ToString() };

            //        foreach (var item in lSearchResultsCache) {
            //            totalResult.SearchResponseRaw += item.SearchResponseRaw;
            //        }
            //        webRequestExt.OnResultReady(totalResult);
            //    }
            //}
        }