示例#1
0
        public void Stream(string tailAPIUrl, Action <Stream> parser)
        {
            var req = SetupConnection(_root.GetAPIUrl(tailAPIUrl));

            if (HasOutput())
            {
                SubmitData(req);
            }
            else if (_method == MethodType.Put)
            {
                req.Headers.Add("Content-Length", "0");
            }

            try
            {
                using (var response = req.GetResponse())
                {
                    using (var stream = response.GetResponseStream())
                    {
                        parser(stream);
                    }
                }
            }
            catch (WebException wex)
            {
                if (wex.Response != null)
                {
                    using (var errorResponse = (HttpWebResponse)wex.Response)
                    {
                        using (var reader = new StreamReader(errorResponse.GetResponseStream()))
                        {
                            string    jsonString = reader.ReadToEnd();
                            JsonError jsonError;
                            try
                            {
                                jsonError = SimpleJson.DeserializeObject <JsonError>(jsonString);
                            }
                            catch (Exception ex)
                            {
                                throw new Exception(string.Format("The remote server returned an error ({0}) with an empty response", errorResponse.StatusCode));
                            }
                            throw new Exception(string.Format("The remote server returned an error ({0}): {1}", errorResponse.StatusCode, jsonError.Message));
                        }
                    }
                }
                else
                {
                    throw wex;
                }
            }
        }
示例#2
0
        public void Stream(string tailAPIUrl, Action <Stream> parser)
        {
            var req = SetupConnection(_root.GetAPIUrl(tailAPIUrl));

            if (HasOutput())
            {
                SubmitData(req);
            }
            else if (_method == MethodType.Put)
            {
                req.Headers.Add("Content-Length", "0");
            }

            using (var response = req.GetResponse())
            {
                using (var stream = response.GetResponseStream())
                {
                    parser(stream);
                }
            }
        }
示例#3
0
        public void Stream(string tailApiUrl, Action <Stream, IDictionary <string, IEnumerable <string> > > parser)
        {
            var client = CreateClient();

            var message = new HttpRequestMessage(new HttpMethod(_method.ToString().ToUpperInvariant()), _root.GetAPIUrl(tailApiUrl));

            if (HasOutput())
            {
                message.Content = new StringContent(SimpleJson.SerializeObject(_data))
                {
                    Headers =
                    {
                        ContentType = new MediaTypeHeaderValue("application/json")
                    }
                };
            }

            var response = client.SendAsync(message).Result;

            using (var content = Read(response))
            {
                if (response.IsSuccessStatusCode == false)
                {
                    using (var reader = new StreamReader(content))
                    {
                        var error = reader.ReadToEnd();
                        if (string.IsNullOrEmpty(error))
                        {
                            throw new Exception(string.Format("The remote server returned an error ({0}) with an empty response", response.StatusCode));
                        }
                        throw new Exception(string.Format("The remote server returned an error ({0}): {1}", response.StatusCode, SimpleJson.DeserializeObject <JsonError>(error).Message));
                    }
                }

                parser(content, response.Headers.ToDictionary(pair => pair.Key, pair => pair.Value));
            }
        }