Example #1
0
        private async Task <HttpStatusCode> PostEvents(
            byte[] serializedEvents,
            CancellationToken cancellationToken)
        {
            // encode data
            HttpResponseMessage response = null;
            string         serverReply   = null;
            HttpStatusCode responseCode  = HttpStatusCode.OK;

            try
            {
                // post data
                HttpEventCollectorHandler postEvents = (t, s) =>
                {
                    HttpContent content = new ByteArrayContent(serializedEvents);
                    content.Headers.ContentType = HttpContentHeaderValue;

                    if (this.applyHttpVersion10Hack)
                    {
                        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, HttpEventCollectorPath);
                        request.Version = HttpVersion.Version10;
                        request.Content = content;

                        return(httpClient.SendAsync(request));
                    }

                    return(httpClient.PostAsync(httpEventCollectorEndpointUri, content));
                };

                response = await postEvents(token, serializedEvents);

                responseCode = response.StatusCode;
                if (responseCode != HttpStatusCode.OK && response.Content != null)
                {
                    // record server reply
                    serverReply = await response.Content.ReadAsStringAsync();

                    OnError(new HttpEventCollectorException(
                                code: responseCode,
                                webException: null,
                                reply: serverReply,
                                response: response,
                                serializedEvents: HttpContentEncoding.GetString(serializedEvents)
                                ));
                }
            }
            catch (HttpEventCollectorException e)
            {
                responseCode       = responseCode == HttpStatusCode.OK ? (e.Response?.StatusCode ?? e.StatusCode) : responseCode;
                e.SerializedEvents = e.SerializedEvents ?? HttpContentEncoding.GetString(serializedEvents);
                OnError(e);
            }
            catch (Exception e)
            {
                responseCode = responseCode == HttpStatusCode.OK ? HttpStatusCode.BadRequest : responseCode;
                OnError(new HttpEventCollectorException(
                            code: responseCode,
                            webException: e,
                            reply: serverReply,
                            response: response,
                            serializedEvents: HttpContentEncoding.GetString(serializedEvents)
                            ));
            }

            return(responseCode);
        }