Esempio n. 1
0
        private void PostToBosun(string path, bool gzip, ApiPostWriter postWriter)
        {
            var url = BosunUrl;

            if (url == null)
            {
                Debug.WriteLine("BosunReporter: BosunUrl is null. Dropping data.");
                return;
            }

            url = new Uri(url, path);

            var request = WebRequest.Create(url);

            request.Method      = "POST";
            request.ContentType = "application/json";
            if (gzip)
            {
                request.Headers["Content-Encoding"] = "gzip";
            }

            // support for http://username:[email protected], by default this does not work
            var userInfo = url.GetComponents(UriComponents.UserInfo, UriFormat.Unescaped);

            if (!string.IsNullOrEmpty(userInfo))
            {
                var auth = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(userInfo));
                request.Headers["Authorization"] = auth;
            }

            try
            {
                using (var stream = request.GetRequestStream())
                {
                    if (gzip)
                    {
                        using (var gzipStream = new GZipStream(stream, CompressionMode.Compress))
                        {
                            postWriter(gzipStream);
                        }
                    }
                    else
                    {
                        postWriter(stream);
                    }
                }

                request.GetResponse().Close();
            }
            catch (WebException e)
            {
                using (var response = (HttpWebResponse)e.Response)
                {
                    if (response == null)
                    {
                        throw new BosunPostException(e);
                    }

                    using (Stream data = response.GetResponseStream())
                        using (var reader = new StreamReader(data))
                        {
                            string text = reader.ReadToEnd();
                            throw new BosunPostException(response.StatusCode, text, e);
                        }
                }
            }
        }
Esempio n. 2
0
        private void PostToBosun(string path, bool gzip, ApiPostWriter postWriter)
        {
            var url = BosunUrl;

            if (url == null)
            {
                Debug.WriteLine("BosunReporter: BosunUrl is null. Dropping data.");
                return;
            }

            url = new Uri(url, path);

            var request = WebRequest.Create(url);

            request.Method      = "POST";
            request.ContentType = "application/json";
            if (gzip)
            {
                request.Headers["Content-Encoding"] = "gzip";
            }

            try
            {
                using (var stream = request.GetRequestStream())
                {
                    if (gzip)
                    {
                        using (var gzipStream = new GZipStream(stream, CompressionMode.Compress))
                            using (var sw = new StreamWriter(gzipStream, new UTF8Encoding(false)))
                            {
                                postWriter(sw);
                            }
                    }
                    else
                    {
                        using (var sw = new StreamWriter(stream, new UTF8Encoding(false)))
                        {
                            postWriter(sw);
                        }
                    }
                }

                request.GetResponse().Close();
            }
            catch (WebException e)
            {
                using (var response = (HttpWebResponse)e.Response)
                {
                    if (response == null)
                    {
                        throw new BosunPostException(e);
                    }

                    using (Stream data = response.GetResponseStream())
                        using (var reader = new StreamReader(data))
                        {
                            string text = reader.ReadToEnd();
                            throw new BosunPostException(response.StatusCode, text, e);
                        }
                }
            }
        }