VS 2010 中没有等待的 HttpClient.PostAsync

Sha*_*air 1 c# asynchronous httpclient async-await

这是这个问题的后续:

如何使用 HTTP POST multipart/form-data 将文件上传到服务器

上传多部分表单数据似乎是一个很好的解决方案。该库可通过 NuGet 在 VS 2010 中使用。

但是,下面的代码使用了await关键字,这在 VS 2010 中不可用。

如果不使用,该代码的正确等价物是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;
Run Code Online (Sandbox Code Playgroud)

Nko*_*osi 5

对响应内容执行相同的操作

HttpResponseMessage response = httpClient.PostAsync("PostUrl", form).Result;
Run Code Online (Sandbox Code Playgroud)