1

This is a follow-up to this question:

How to upload file to server with HTTP POST multipart/form-data

It seems to be a good solution that uploads multipart form data. The library is available in VS 2010 through NuGet.

However, the code below uses await keyword, which is unavailable in VS 2010.

What would be a correct equivalent of that code without using await?

HttpClient httpClient = new HttpClient();
MultipartFormDataContent form = new MultipartFormDataContent();

form.Add(new StringContent(username), "username");
form.Add(new StringContent(useremail), "email");
form.Add(new StringContent(password), "password");
form.Add(new StringContent(usertype), "user_type");
form.Add(new StringContent(subjects), "subjects");
form.Add(new ByteArrayContent(imagebytearraystring, 0, imagebytearraystring.Count()), "profile_pic", "hello1.jpg");
HttpResponseMessage response = await httpClient.PostAsync("PostUrl", form);

response.EnsureSuccessStatusCode();
httpClient.Dispose();
string sd = response.Content.ReadAsStringAsync().Result;
Community
  • 1
  • 1
SharpAffair
  • 5,450
  • 12
  • 71
  • 152

1 Answers1

5

Do the same thing you did for the response content

HttpResponseMessage response = httpClient.PostAsync("PostUrl", form).Result;
Nkosi
  • 191,971
  • 29
  • 311
  • 378
  • Hi Nkosi, I am also using the same code without await keyword. It serves more than 400K users. But sometimes it gets this exception. What causes this? Have any idea? : System.Threading.Tasks.TaskCanceledException: A task was canceled. – Sabri Meviş Sep 28 '18 at 08:12