예제 #1
0
        /// <summary>
        /// Executes the HTTP request to Sentry.
        /// </summary>
        /// <returns>
        /// The <see cref="JsonPacket.EventID" /> of the successfully captured JSON packet, or <c>null</c> if it fails.
        /// </returns>
        public string Request()
        {
            using (var s = this.webRequest.GetRequestStreamAsync().Result)
            {
                if (this.ravenClient.Compression)
                {
                    GzipUtil.Write(this.data.Scrubbed, s);
                }
                else
                {
                    using (var sw = new StreamWriter(s))
                    {
                        sw.Write(this.data.Scrubbed);
                    }
                }
            }

            using (var wr = (HttpWebResponse)this.webRequest.GetResponseAsync().Result)
            {
                using (var responseStream = wr.GetResponseStream())
                {
                    if (responseStream == null)
                    {
                        return(null);
                    }

                    using (var sr = new StreamReader(responseStream))
                    {
                        var content = sr.ReadToEnd();
                        #if (net35)
                        var response = JObject.Parse(content);
                        return(response["id"] != null ? response["id"].ToString() : null);
                        #else
                        var response = JsonConvert.DeserializeObject <dynamic>(content);
                        return(response.id);
                        #endif
                    }
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Sends the specified packet to Sentry.
        /// </summary>
        /// <param name="packet">The packet to send.</param>
        /// <param name="dsn">The Data Source Name in Sentry.</param>
        /// <returns>
        /// The <see cref="JsonPacket.EventID"/> of the successfully captured JSON packet, or <c>null</c> if it fails.
        /// </returns>
        protected virtual string Send(JsonPacket packet, Dsn dsn)
        {
            packet.Logger = Logger;

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(dsn.SentryUri);
                request.Method = "POST";
                request.Accept = "application/json";
                request.Headers.Add("X-Sentry-Auth", PacketBuilder.CreateAuthenticationHeader(dsn));
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

                // Added to disable self signed certificate
                ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateRemoteCertificate);

                request.UserAgent = PacketBuilder.UserAgent;

                if (Compression)
                {
                    request.Headers.Add(HttpRequestHeader.ContentEncoding, "gzip");
                    request.AutomaticDecompression = DecompressionMethods.Deflate;
                    request.ContentType            = "application/octet-stream";
                }
                else
                {
                    request.ContentType = "application/json; charset=utf-8";
                }

                /*string data = packet.ToString(Formatting.Indented);
                 * Console.WriteLine(data);*/

                string data = packet.ToString(Formatting.None);

                if (LogScrubber != null)
                {
                    data = LogScrubber.Scrub(data);
                }

                // Write the messagebody.
                using (Stream s = request.GetRequestStream())
                {
                    if (Compression)
                    {
                        GzipUtil.Write(data, s);
                    }
                    else
                    {
                        using (StreamWriter sw = new StreamWriter(s))
                            sw.Write(data);
                    }
                }

                using (HttpWebResponse wr = (HttpWebResponse)request.GetResponse())
                    using (Stream responseStream = wr.GetResponseStream())
                    {
                        if (responseStream == null)
                        {
                            return(null);
                        }

                        using (StreamReader sr = new StreamReader(responseStream))
                        {
                            string content  = sr.ReadToEnd();
                            var    response = JsonConvert.DeserializeObject <dynamic>(content);
                            return(response.id);
                        }
                    }
            }
            catch (Exception exception)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write("[ERROR] ");
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine(exception);

                WebException webException = exception as WebException;
                if (webException != null && webException.Response != null)
                {
                    string messageBody;
                    using (Stream stream = webException.Response.GetResponseStream())
                    {
                        if (stream == null)
                        {
                            return(null);
                        }

                        using (StreamReader sw = new StreamReader(stream))
                            messageBody = sw.ReadToEnd();
                    }

                    Console.WriteLine("[MESSAGE BODY] " + messageBody);
                }
            }

            return(null);
        }
예제 #3
0
        /// <summary>
        /// Sends the specified packet to Sentry.
        /// </summary>
        /// <param name="packet">The packet to send.</param>
        /// <param name="dsn">The Data Source Name in Sentry.</param>
        /// <returns>
        /// The <see cref="JsonPacket.EventID"/> of the successfully captured JSON packet, or <c>null</c> if it fails.
        /// </returns>
        protected virtual string Send(JsonPacket packet, Dsn dsn)
        {
            packet.Logger = Logger;

            var request = (HttpWebRequest)WebRequest.Create(dsn.SentryUri);

            request.Timeout          = (int)Timeout.TotalMilliseconds;
            request.ReadWriteTimeout = (int)Timeout.TotalMilliseconds;
            request.Method           = "POST";
            request.Accept           = "application/json";
            request.Headers.Add("X-Sentry-Auth", PacketBuilder.CreateAuthenticationHeader(dsn));
            request.UserAgent = PacketBuilder.UserAgent;

            if (Compression)
            {
                request.Headers.Add(HttpRequestHeader.ContentEncoding, "gzip");
                request.AutomaticDecompression = DecompressionMethods.Deflate;
                request.ContentType            = "application/octet-stream";
            }
            else
            {
                request.ContentType = "application/json; charset=utf-8";
            }

            /*string data = packet.ToString(Formatting.Indented);
             * Console.WriteLine(data);*/

            string data = packet.ToString(Formatting.None);

            if (LogScrubber != null)
            {
                data = LogScrubber.Scrub(data);
            }

            // Write the messagebody.
            using (Stream s = request.GetRequestStream())
            {
                if (Compression)
                {
                    GzipUtil.Write(data, s);
                }
                else
                {
                    using (StreamWriter sw = new StreamWriter(s))
                        sw.Write(data);
                }
            }

            using (HttpWebResponse wr = (HttpWebResponse)request.GetResponse())
                using (Stream responseStream = wr.GetResponseStream())
                {
                    if (responseStream == null)
                    {
                        return(null);
                    }

                    using (StreamReader sr = new StreamReader(responseStream))
                    {
                        string content  = sr.ReadToEnd();
                        var    response = JsonConvert.DeserializeObject <dynamic>(content);
                        return(response.id);
                    }
                }
        }
예제 #4
0
        /// <summary>Sends the specified packet to Sentry.</summary>
        /// <param name="packet">The packet to send.</param>
        /// <returns>
        /// The <see cref="JsonPacket.EventID" /> of the successfully captured JSON packet, or <c>null</c> if it fails.
        /// </returns>
        protected virtual string Send(JsonPacket packet)
        {
            try
            {
                // TODO(dcramer): moving this out of Send makes it easier to test the final
                // generated packet
                packet = PreparePacket(packet);

                var request = (HttpWebRequest)WebRequest.Create(this.currentDsn.SentryUri);
                request.Timeout          = (int)Timeout.TotalMilliseconds;
                request.ReadWriteTimeout = (int)Timeout.TotalMilliseconds;
                request.Method           = "POST";
                request.Accept           = "application/json";
                request.Headers.Add("X-Sentry-Auth", PacketBuilder.CreateAuthenticationHeader(this.currentDsn));
                request.UserAgent = PacketBuilder.UserAgent;

                if (Compression)
                {
                    request.Headers.Add(HttpRequestHeader.ContentEncoding, "gzip");
                    request.AutomaticDecompression = DecompressionMethods.Deflate;
                    request.ContentType            = "application/octet-stream";
                }
                else
                {
                    request.ContentType = "application/json; charset=utf-8";
                }

                /*string data = packet.ToString(Formatting.Indented);
                 *  Console.WriteLine(data);*/

                var data = packet.ToString(Formatting.None);

                if (LogScrubber != null)
                {
                    data = LogScrubber.Scrub(data);
                }

                // Write the messagebody.
                using (var s = request.GetRequestStream())
                {
                    if (Compression)
                    {
                        GzipUtil.Write(data, s);
                    }
                    else
                    {
                        using (var sw = new StreamWriter(s))
                        {
                            sw.Write(data);
                        }
                    }
                }

                using (var wr = (HttpWebResponse)request.GetResponse())
                {
                    using (var responseStream = wr.GetResponseStream())
                    {
                        if (responseStream == null)
                        {
                            return(null);
                        }

                        using (var sr = new StreamReader(responseStream))
                        {
                            var content  = sr.ReadToEnd();
                            var response = JsonConvert.DeserializeObject <dynamic>(content);
                            return(response.id);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return(HandleException(ex));
            }
        }