Esempio n. 1
0
        public RssItem[] EndGetRss(IAsyncResult asyncResult)
        {
            CustomAsyncResult <WebRequestState> myAsyncResult = (CustomAsyncResult <WebRequestState>)asyncResult;
            WebRequestState myState = myAsyncResult.AdditionalData;

            var result = RssHelper.ConvertXmlToRss(XElement.Load(new XmlTextReader(CompleteGetUrl(asyncResult))), myState.Count).ToArray();

            WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
            return(result);
        }
Esempio n. 2
0
        private Stream CompleteGetUrl(IAsyncResult asyncResult)
        {
            if (asyncResult is CompletedAsyncResult <WebRequestState> )
            {
                /// The content is already in cache. So, return the item from
                /// cache
                WebRequestState myState = (asyncResult as CompletedAsyncResult <WebRequestState>).Data;
                string          content = Services.Get <ICache>().Get(myState.Url) as string;
                return(new MemoryStream(Encoding.UTF8.GetBytes(content)));
            }
            else
            {
                CustomAsyncResult <WebRequestState> myAsyncResult = (CustomAsyncResult <WebRequestState>)asyncResult;
                WebRequestState myState = myAsyncResult.AdditionalData;

                if (myState.Error != null)
                {
                    throw new ProtocolException(myState.Error.Message, myState.Error);
                }
                //throw new WebProtocolException(HttpStatusCode.InternalServerError, myState.Error.Message,
                //myState.Error);

                var outResponse = WebOperationContext.Current.OutgoingResponse;
                outResponse.ContentLength = myState.Response.ContentLength;
                outResponse.ContentType   = myState.Response.ContentType;
                SetCaching(WebOperationContext.Current, DateTime.Now, myState.CacheDuration);

                var contentEncoding = myState.Response.ContentEncoding;
                if (myState.CacheDuration > 0)
                {
                    return(new StreamWrapper(myState.Response.GetResponseStream(),
                                             (int)(outResponse.ContentLength > 0 ? outResponse.ContentLength : 8 * 1024),
                                             buffer =>
                    {
                        Encoding enc;
                        try
                        {
                            if (string.IsNullOrEmpty(contentEncoding))
                            {
                                enc = Encoding.UTF8;
                            }
                            else
                            {
                                enc = Encoding.GetEncoding(contentEncoding);
                            }
                        }
                        catch
                        {
                            enc = Encoding.GetEncoding(1252);
                        }

                        if (!ConstantHelper.DisableCache)
                        {
                            Services.Get <ICache>().Add(myState.Url, enc.GetString(buffer));
                        }
                    }));
                }
                else
                {
                    return(myState.Response.GetResponseStream());
                }
            }
        }