Tell me, is it possible to do without asynchronous await/async operations in the code to load the file?

Resource is used to download the file: https://anonfile.com/docs/api

enter image description here

To work with the API resource provides the code:

 curl -F "file=@test.txt" https://anonfile.com/?token=s245v7ebt3sn7kf5 

The curl code converted to C# on the service: https://curl.olsh.me/

Got result:

 using (var httpClient = new HttpClient()) { using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://anonfile.com/?token=s245v7ebt3sn7kf5")) { var multipartContent = new MultipartFormDataContent(); multipartContent.Add(new ByteArrayContent(File.ReadAllBytes("test.txt")), "file", Path.GetFileName("test.txt")); request.Content = multipartContent; var response = await httpClient.SendAsync(request); } } 

The problem in the code is the last line:

 var response = await httpClient.SendAsync(request); 

When I insert this code into the button, I have to add async and use version NetFramework 4.5+

 private async void button1_Click(object sender, EventArgs e) 

Is it possible to get rid of await/async so that the file is loaded in another way?

  • 3
    Remove await, add .Result - PashaPash
  • one
    Just plug in async await support for .NET 4.0 at stackoverflow.com/a/19426866/3786094 - CasperSC 9:51 am
  • one
    This is an outdated way to block the stream incorrectly, but if you really need, then using (var wc = new WebClient()) wc.DownloadFile("url", "file_path"); - tym32167
  • one
    Remove from the async method, and then just write var response = httpClient.SendAsync(request).GetAwaiter().GetResult(); - Bulson

2 answers 2

Keep a helper, helping to respond to the answer without serious consequences

 using System; using System.IO; using System.Net.Http; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void OnButtonClick(object sender, EventArgs e) { using (var httpClient = new HttpClient()) { using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://anonfile.com/?token=s245v7ebt3sn7kf5")) { var multipartContent = new MultipartFormDataContent(); multipartContent.Add( new ByteArrayContent(File.ReadAllBytes("test.txt")), "file", Path.GetFileName("test.txt")); request.Content = multipartContent; Helper.Send(request, task => { if (task.Status == TaskStatus.RanToCompletion) { SetResult(task.Result.ToString()); } else { SetResult("Что-то пошло не так"); } }, httpClient); } } } private void SetResult(string text) { if (InvokeRequired) { Invoke(new Action(() => { SetResult(text); })); } else { Text = text; } } } public static class Helper { public static void Send(HttpRequestMessage request, Action<Task<HttpResponseMessage>> continueAction, HttpClient httpClient) { SendRequest(request, continueAction, httpClient); } private static void SendRequest(HttpRequestMessage request, Action<Task<HttpResponseMessage>> continueAction, HttpClient httpClient) { var task = httpClient.SendAsync(request); task.GetAwaiter().OnCompleted(() => { continueAction(task); }); } } } 
  • I get the answer: Что-то пошло не так immediately after pressing the button ... - Vitokhv
  • So you need to debug step by step and watch the answer. The question would be how to remove await, and not about why the request does not pass. - RepetitorDev

You can somehow handle the response asynchronously so that the window does not hang.

  private void OnButtonClick(object sender, EventArgs e) { using (var httpClient = new HttpClient()) { using (var request = new HttpRequestMessage( new HttpMethod("POST"), "https://someserver.com/?token=s123")) { var multipartContent = new MultipartFormDataContent(); multipartContent.Add(new ByteArrayContent( File.ReadAllBytes("test.txt")), "file", Path.GetFileName("test.txt")); request.Content = multipartContent; httpClient.SendAsync(request).ContinueWith(task => { if (task.Status == TaskStatus.RanToCompletion) { //continuation(task.Result); } else { // ... //continuation(null); } }, TaskScheduler.FromCurrentSynchronizationContext()); } } }