private BacktraceResult Send(Guid requestId, string json, List <string> attachments, BacktraceReport report, int deduplication = 0) { var requestUrl = _serverurl.ToString(); if (deduplication > 0) { requestUrl += $"&_mod_duplicate={deduplication}"; } var formData = FormDataHelper.GetFormData(json, attachments, requestId); string contentType = FormDataHelper.GetContentTypeWithBoundary(requestId); var request = WebRequest.Create(requestUrl) as HttpWebRequest; //Set up the request properties. request.Method = "POST"; request.ContentType = contentType; request.ContentLength = formData.Length; try { using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(formData, 0, formData.Length); requestStream.Close(); } return(ReadServerResponse(request, report)); } catch (Exception exception) { OnServerError?.Invoke(exception); return(BacktraceResult.OnError(report, exception)); } #endif }
internal async Task <BacktraceResult> SendAsync(Guid requestId, string json, List <string> attachments, BacktraceReport report, int deduplication = 0) { string contentType = FormDataHelper.GetContentTypeWithBoundary(requestId); string boundary = FormDataHelper.GetBoundary(requestId); using (var content = new MultipartFormDataContent(boundary)) { var requestUrl = _serverurl.ToString(); if (deduplication > 0) { requestUrl += $"&_mod_duplicate={deduplication}"; } var request = new HttpRequestMessage(HttpMethod.Post, requestUrl); content.AddJson("upload_file.json", json); content.AddFiles(attachments); //// clear and add content type with boundary tag content.Headers.Remove("Content-Type"); content.Headers.TryAddWithoutValidation("Content-Type", contentType); request.Content = content; try { using (var response = await HttpClient.SendAsync(request)) { var fullResponse = await response.Content.ReadAsStringAsync(); if (response.StatusCode != HttpStatusCode.OK) { var err = new WebException(response.ReasonPhrase); System.Diagnostics.Trace.WriteLine(fullResponse); OnServerError?.Invoke(err); return(BacktraceResult.OnError(report, err)); } var result = JsonConvert.DeserializeObject <BacktraceResult>(fullResponse); result.BacktraceReport = report; OnServerResponse?.Invoke(result); return(result); } } catch (Exception exception) { System.Diagnostics.Trace.WriteLine($"Backtrace - Server error: {exception.ToString()}"); OnServerError?.Invoke(exception); return(BacktraceResult.OnError(report, exception)); } } }
public async Task <BacktraceResult> SendAsync(BacktraceData data) { //check rate limiting bool watcherValidation = reportLimitWatcher.WatchReport(data.Report); if (!watcherValidation) { return(BacktraceResult.OnLimitReached(data.Report)); } // execute user custom request handler if (RequestHandler != null) { return(RequestHandler?.Invoke(_serverurl.ToString(), FormDataHelper.GetContentTypeWithBoundary(Guid.NewGuid()), data)); } //get a json from diagnostic object var json = JsonConvert.SerializeObject(data, JsonSerializerSettings); return(await SendAsync(Guid.NewGuid(), json, data.Attachments, data.Report, data.Deduplication)); }
/// <summary> /// Sending a diagnostic report data to server API. /// </summary> /// <param name="data">Diagnostic data</param> /// <returns>Server response</returns> public BacktraceResult Send(BacktraceData data) { #if !NET35 return(Task.Run(() => SendAsync(data)).Result); #else //check rate limiting bool watcherValidation = reportLimitWatcher.WatchReport(data.Report); if (!watcherValidation) { return(BacktraceResult.OnLimitReached(data.Report)); } // execute user custom request handler if (RequestHandler != null) { return(RequestHandler?.Invoke(_serverurl.ToString(), FormDataHelper.GetContentTypeWithBoundary(Guid.NewGuid()), data)); } //set submission data string json = JsonConvert.SerializeObject(data); return(Send(Guid.NewGuid(), json, data.Report?.AttachmentPaths ?? new List <string>(), data.Report, data.Deduplication)); }