Пример #1
0
        public IEnumerable <SearchResult> EndQuery(IAsyncResult result)
        {
            if (!result.IsCompleted)
            {
                result.AsyncWaitHandle.WaitOne();
            }

            BingResult bingResult = result as BingResult;

            if (bingResult == null)
            {
                throw new ArgumentException(InvalidAsyncResult, "result");
            }

            WebRequest req = bingResult.InnerResult.AsyncState as WebRequest;

            if (req == null)
            {
                throw new ArgumentException(InvalidAsyncResult, "result");
            }

            XElement            root;
            XmlNamespaceManager nsmgr;

            using (WebResponse resp = req.EndGetResponse(bingResult.InnerResult))
                using (Stream stream = resp.GetResponseStream())
                {
                    XmlReader reader = XmlReader.Create(stream);
                    root  = XElement.Load(reader);
                    nsmgr = new XmlNamespaceManager(reader.NameTable);
                }

            // Extend the namespace manager with some prefixes needed for processing results.
            nsmgr.AddNamespace(SearchApiPrefix, SearchApiNS);
            nsmgr.AddNamespace(SearchWebPrefix, SearchWebNS);

            var errors = root.XPathSelectElements(SearchErrorXPath, nsmgr);

            if (errors.Count() > 0)
            {
                // TODO:  Probably want to report these somewhere.
                throw new ApplicationException("Search service errors were returned.");
            }

            var webResults = root.XPathSelectElements(SearchWebResultXPath, nsmgr);

            foreach (var webResult in webResults)
            {
                var title       = webResult.XPathSelectElement(SearchWebResultTitle, nsmgr);
                var url         = webResult.XPathSelectElement(SearchWebResultUrl, nsmgr);
                var description = webResult.XPathSelectElement(SearchWebResultDescription, nsmgr);
                yield return(new WebResult()
                {
                    Title = title == null ? null : title.Value,
                    Url = url == null ? null : url.Value,
                    Description = description == null ? null : description.Value
                });
            }
        }
Пример #2
0
        public IAsyncResult BeginQuery(string query, SourceType sources, AsyncCallback callback, object state)
        {
            string     uri = ConstructQueryUri(query, sources);
            WebRequest req = WebRequest.Create(uri);

            BingResult bingResult = new BingResult(state);

            bingResult.InnerResult = req.BeginGetResponse(
                (result) =>
            {
                if (callback != null)
                {
                    callback(bingResult);
                }
            },
                req);

            return(bingResult);
        }
Пример #3
0
        public IAsyncResult BeginQuery(string query, SourceType sources, AsyncCallback callback, object state)
        {
            string uri = ConstructQueryUri(query, sources);
            WebRequest req = WebRequest.Create(uri);

            BingResult bingResult = new BingResult(state);
            bingResult.InnerResult = req.BeginGetResponse(
                (result) =>
                {
                    if (callback != null)
                        callback(bingResult);
                },
                req);

            return bingResult;
        }