private async void UploadHttpClient(PictureData pictureData, string namePicture, bool retry = true) { pictureData.screenshotData.StartUpload = DateTime.Now; try { using (var client = new HttpClient()) { client.Timeout = TimeSpan.FromSeconds(60); using (var content = new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture))) { content.Add(new StreamContent(new MemoryStream(pictureData.DataBytes)), "fichier", namePicture); using (var message = await client.PostAsync("http://www.noelshack.com/api.php", content)) { var reponse = await message.Content.ReadAsStringAsync(); if (!reponse.Contains("http://www.noelshack.com")) { if (retry) this.UploadHttpClient(pictureData, namePicture, false); else this.manager.UploadFailed(); return; } pictureData.screenshotData.StopUpload = DateTime.Now; this.manager.Uploaded(pictureData.Picture, this.CustomUrl(reponse), pictureData.screenshotData, false); } } } } catch (Exception e) { this.logger.Error(e.Message); if (e.Message == "A task was canceled.") { this.manager.UploadPictureTooLarge(); } else { if (retry) this.UploadHttpClient(pictureData, namePicture, false); else this.manager.ConnexionFailed(); } } }
// Upload first picture public void Upload(PictureData pictureData) { var namePicture = new Random().Next(0, 9999).ToString("0000") + "-" + Properties.Resources.NamePicture + "." + pictureData.GetSmallerFormat(); this.UploadHttpClient(pictureData, namePicture); }
public void Captured(Bitmap img, ScreenshotData screenshotData, bool upload) { this.pressCounter = 0; if (upload) { this.notifyIconViewModel.EnableCommands(false); var pictureData = new PictureData(img, screenshotData); new UploaderService(this).Upload(pictureData); } else { Application.Current.Dispatcher.Invoke(() => Clipboard.SetImage(CreateBitmapSourceFromBitmap(img))); this.notifyIconViewModel.ShowPopupCopy(img); } }