在VS 2010中没有等待的HttpClient.PostAsync

时间:2016-11-06 12:38:41

标签: c# asynchronous async-await httpclient

这是这个问题的后续行动:

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

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

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

在不使用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; 的情况下,该代码的正确等价是什么?

{{1}}

1 个答案:

答案 0 :(得分:3)

为响应内容做同样的事情

HttpResponseMessage response = httpClient.PostAsync("PostUrl", form).Result;