Ejemplo n.º 1
0
        protected virtual IAsyncResult ExecutePostOrPutAsync(PostOrPut method, string url, string prefixKey,
                                                             IClientCache cache, TimeSpan slidingExpiration)
        {
            WebResponse = null;

            byte[] content;
            var request = BuildPostOrPutWebRequest(method, url, out content);

            var state = new Pair<WebRequest, Pair<byte[], Triplet<IClientCache, TimeSpan, string>>>
                            {
                                First = request,
                                Second = new Pair<byte[], Triplet<IClientCache, TimeSpan, string>>
                                             {
                                                 First = content,
                                                 Second = new Triplet<IClientCache, TimeSpan, string>
                                                              {
                                                                  First = cache,
                                                                  Second = slidingExpiration,
                                                                  Third = prefixKey
                                                              }
                                             }
                            };

            var args = new WebQueryRequestEventArgs(url);
            OnQueryRequest(args);

            return request.BeginGetRequestStream(PostAsyncRequestCallback, state);
        }
Ejemplo n.º 2
0
        protected virtual void ExecutePostOrPutAsync(PostOrPut method, string url,
                                                     IEnumerable<HttpPostParameter> parameters)
        {
            WebResponse = null;

            // Credit to Sean Erickson for providing a clean 
            // implementation of multi-part data posting
            byte[] content;
            var request = BuildMultiPartFormRequest(method, url, parameters, out content);

            var state = new Pair<WebRequest, byte[]> {First = request, Second = content};

            var args = new WebQueryRequestEventArgs(url);
            OnQueryRequest(args);

            request.BeginGetRequestStream(PostAsyncRequestCallback, state);
        }
Ejemplo n.º 3
0
        // expects real cache key, not prefix
        private void ExecuteGetAsyncAndCacheWithExpiry(IClientCache cache, string key, string url,
                                                       TimeSpan slidingExpiration, IWebQueryClient client)
        {
            var fetch = cache.Get<string>(key);

            if (fetch != null)
            {
                var args = new WebQueryResponseEventArgs(fetch);
                OnQueryResponse(args);
            }
            else
            {
                var state = new Pair<IClientCache, Pair<string, TimeSpan>>
                                {
                                    First = cache,
                                    Second = new Pair<string, TimeSpan> {First = key, Second = slidingExpiration}
                                };

                var args = new WebQueryRequestEventArgs(url);
                OnQueryRequest(args);

                client.OpenReadCompleted += client_OpenReadCompleted;
                client.OpenReadAsync(new Uri(url), state);
            }
        }
Ejemplo n.º 4
0
        protected virtual void ExecuteGetAsync(string url)
        {
            WebResponse = null;

            var client = CreateWebQueryClient();

            client.OpenReadCompleted += client_OpenReadCompleted;
            try
            {
                var args = new WebQueryRequestEventArgs(url);
                OnQueryRequest(args);

                client.OpenReadAsync(new Uri(url));
            }
            catch (WebException ex)
            {
                client.Exception = ex;
                HandleWebException(ex);
            }
            catch (NullReferenceException)
            {
                // Can happen on DNS failures following a WebException.
                // Client should already have the exception info for the WebException
            }
        }
Ejemplo n.º 5
0
        protected virtual IAsyncResult ExecutePostOrPutAsync(PostOrPut method, string url)
        {
            WebResponse = null;

            byte[] content;
            var request = BuildPostOrPutWebRequest(method, url, out content);

            var state = new Pair<WebRequest, byte[]> {First = request, Second = content};

            var args = new WebQueryRequestEventArgs(url);
            OnQueryRequest(args);

            return request.BeginGetRequestStream(PostAsyncRequestCallback, state);
        }
Ejemplo n.º 6
0
        protected virtual string ExecuteGet(string url, out WebException exception)
        {
            WebResponse = null;
            var client = CreateWebQueryClient();

            var requestArgs = new WebQueryRequestEventArgs(url);
            OnQueryRequest(requestArgs);

            try
            {
                using (var stream = client.OpenRead(url))
                {
                    using (var reader = new StreamReader(stream))
                    {
                        var result = reader.ReadToEnd();

                        var responseArgs = new WebQueryResponseEventArgs(result);
                        OnQueryResponse(responseArgs);

                        return result;
                    }
                }
            }
            catch (WebException ex)
            {
                client.Exception = client.Exception ?? ex; 
                return HandleWebException(ex);
            }
            finally
            {
                exception = client.Exception;
                WebResponse = client.Response;
            }
        }
Ejemplo n.º 7
0
        protected virtual string ExecutePostOrPut(PostOrPut method, string url, out WebException exception)
        {
            WebResponse = null;
            exception = null;
            byte[] content;
            var request = BuildPostOrPutWebRequest(method, url, out content);

            var requestArgs = new WebQueryRequestEventArgs(url);
            OnQueryRequest(requestArgs);

            try
            {
                using (var stream = request.GetRequestStream())
                {
                    stream.Write(content, 0, content.Length);
                    stream.Close();

                    // Avoid disposing until no longer needed to build results
                    var response = request.GetResponse();
                    WebResponse = response;

                    using (var reader = new StreamReader(response.GetResponseStream()))
                    {
                        var result = reader.ReadToEnd();

                        var responseArgs = new WebQueryResponseEventArgs(result);
                        OnQueryResponse(responseArgs);

                        return result;
                    }
                }
            }
            catch (WebException ex)
            {
                exception = ex; 
                return HandleWebException(ex);
            }
        }
Ejemplo n.º 8
0
        public virtual void ExecuteStreamGet(string url, TimeSpan duration, int resultCount)
        {
            WebResponse = null;
            var client = CreateWebQueryClient();

            var requestArgs = new WebQueryRequestEventArgs(url);
            OnQueryRequest(requestArgs);

            Stream stream = null;

            try
            {
                using (stream = client.OpenRead(url))
                {
                    // [DC]: cannot refactor this block to common method; will cause wc/hwr to hang
                    var count = 0;
                    var results = new List<string>();
                    var start = DateTime.UtcNow;

                    using (var reader = new StreamReader(stream))
                    {
                        var line = "";

                        while ((line = reader.ReadLine()).Length > 0)
                        {
                            if (line.Equals(Environment.NewLine))
                            {
                                // Keep-Alive
                                continue;
                            }

                            if (line.Equals("<html>"))
                            {
                                // We're looking at a 401 or similar; construct error result?
                                return;
                            }

                            results.Add(line);
                            count++;

                            if (count < resultCount)
                            {
                                // Result buffer
                                continue;
                            }

                            var sb = new StringBuilder();
                            foreach (var result in results)
                            {
                                sb.AppendLine(result);
                            }

                            var responseArgs = new WebQueryResponseEventArgs(sb.ToString());
                            OnQueryResponse(responseArgs);

                            count = 0;

                            var now = DateTime.UtcNow;
                            if (duration == TimeSpan.Zero || now.Subtract(start) < duration)
                            {
                                continue;
                            }

                            // Time elapsed
                            client.CancelAsync();
                            return;
                        }

                        // Stream dried up
                    }
                    client.CancelAsync();
                }
            }
            catch (WebException ex)
            {
                client.Exception = client.Exception ?? ex;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                }

                WebResponse = client.Response;
            }
        }
Ejemplo n.º 9
0
        public virtual void ExecuteStreamPost(PostOrPut method, string url, TimeSpan duration, int resultCount)
        {
            WebResponse = null;
            byte[] content;
            var request = BuildPostOrPutWebRequest(method, url, out content);

            var requestArgs = new WebQueryRequestEventArgs(url);
            OnQueryRequest(requestArgs);

            Stream stream = null;
            try
            {
                using (stream = request.GetRequestStream())
                {
                    stream.Write(content, 0, content.Length);
                    stream.Close();

                    var response = request.GetResponse();
                    WebResponse = response;
                    
                    using (var responseStream = response.GetResponseStream())
                    {
                        // [DC]: cannot refactor this block to common method; will cause hwr to hang
                        var count = 0;
                        var results = new List<string>();
                        var start = DateTime.UtcNow;

                        using (var reader = new StreamReader(responseStream))
                        {
                            var line = "";

                            while ((line = reader.ReadLine()).Length > 0)
                            {
                                if (line.Equals(Environment.NewLine))
                                {
                                    // Keep-Alive
                                    continue;
                                }

                                if (line.Equals("<html>"))
                                {
                                    // We're looking at a 401 or similar; construct error result?
                                    return;
                                }

                                results.Add(line);
                                count++;

                                if (count < resultCount)
                                {
                                    // Result buffer
                                    continue;
                                }

                                var sb = new StringBuilder();
                                foreach (var result in results)
                                {
                                    sb.AppendLine(result);
                                }

                                var responseArgs = new WebQueryResponseEventArgs(sb.ToString());
                                OnQueryResponse(responseArgs);

                                count = 0;

                                var now = DateTime.UtcNow;
                                if (now.Subtract(start) >= duration)
                                {
                                    // Time elapsed
                                    request.Abort();
                                    return;
                                }
                            }

                            // Stream dried up
                        }
                    }
                }
            }
            catch (WebException)
            {
                // 
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                }
            }
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Raises the <see cref="QueryRequest"/> event.
 /// </summary>
 /// <param name="args">The <see cref="TweetSharp.Core.Web.WebQueryRequestEventArgs"/> instance containing the event data.</param>
 public virtual void OnQueryRequest(WebQueryRequestEventArgs args)
 {
     if (QueryRequest != null)
     {
         QueryRequest(this, args);
     }
 }
Ejemplo n.º 11
0
        protected virtual IAsyncResult ExecuteDeleteAsync(string url, string prefixKey, IClientCache cache,
                                                          DateTime absoluteExpiration)
        {
            WebResponse = null;

            var request = BuildDeleteWebRequest(url);

            var state = new Pair<WebRequest, Pair<byte[], Triplet<IClientCache, DateTime, string>>>
                            {
                                First = request,
                                Second = new Pair<byte[], Triplet<IClientCache, DateTime, string>>
                                             {
                                                 First = null,
                                                 Second = new Triplet<IClientCache, DateTime, string>
                                                              {
                                                                  First = cache,
                                                                  Second = absoluteExpiration,
                                                                  Third = prefixKey
                                                              }
                                             }
                            };

            var args = new WebQueryRequestEventArgs(url);
            OnQueryRequest(args);

            return request.BeginGetRequestStream(PostAsyncRequestCallback, state);
        }
Ejemplo n.º 12
0
        protected virtual IAsyncResult ExecuteDeleteAsync(string url, string key, IClientCache cache)
        {
            WebResponse = null;

            var request = BuildDeleteWebRequest(url);
            var state = new Pair<WebRequest, Triplet<byte[], IClientCache, string>>
                            {
                                First = request,
                                Second = new Triplet<byte[], IClientCache, string>
                                             {
                                                 First = null,
                                                 Second = cache,
                                                 Third = key
                                             }
                            };

            var args = new WebQueryRequestEventArgs(url);
            OnQueryRequest(args);

            return request.BeginGetRequestStream(PostAsyncRequestCallback, state);
        }
Ejemplo n.º 13
0
        // todo rather than always override this, use SetAuthorizationHeader with a virtual no-op
        protected virtual IAsyncResult ExecuteDeleteAsync(string url)
        {
            WebResponse = null;

            var request = BuildDeleteWebRequest(url);

            var state = new Pair<WebRequest, byte[]> {First = request, Second = null};
            var args = new WebQueryRequestEventArgs(url);
            OnQueryRequest(args);

            return request.BeginGetRequestStream(PostAsyncRequestCallback, state);
        }
Ejemplo n.º 14
0
        protected override string ExecuteGet(string url, out WebException exception)
        {
            WebResponse = null;
            exception = null;
            
            var client = CreateWebQueryClient();
            if (HasAuth)
            {
                client.WebCredentials = new WebCredentials(_username, _password);
            }

            var requestArgs = new WebQueryRequestEventArgs(url);
            OnQueryRequest(requestArgs);

            // todo too much code duplication with base class
            try
            {
                using (var stream = client.OpenRead(url))
                {
                    exception = client.Exception; 
                    using (var reader = new StreamReader(stream))
                    {
                        var result = reader.ReadToEnd();
                        var responseArgs = new WebQueryResponseEventArgs(result);
                        OnQueryResponse(responseArgs);

                        return result;
                    }
                }
            }
            catch (WebException ex)
            {
                exception = client.Exception ?? ex;
                return HandleWebException(ex);
            }
            finally
            {
                if (WebResponse == null || client.Response != null)
                {
                    WebResponse = client.Response;
                }
                if ( WebResponse == null && client.Request != null )
                {
                    WebResponse = client.GetWebResponseShim(client.Request);
                }
            }
        }