尝试使用httpClient和我的应用程序发布帖子我有下面这行代码
public ActionResult Create(LutUsers users)
{
HttpResponseMessage response = client.PostAsync(client.BaseAddress + "/user/create",
**httpcontent**).Result;
if (response.IsSuccessStatusCode)
{
}
return View();
}我的httpcontent应该是什么?我正在使用web api发布我的数据
THank感谢你的帮助
发布于 2021-05-27 06:04:17
如果你可以对它使用异步方法,你可以像这样使用它:
HttpClient client = new HttpClient();
StringContent postContent = new StringContent("{ \"firstName\": \"John\" }");
var response = await client.PostAsync(client.BaseAddress + "/user/create", postContent);
var resString = await response.Content.ReadAsStringAsync();如果您需要同步方法,您可以为其创建任务:
HttpClient client = new HttpClient();
StringContent postContent = new StringContent(content);
var task = Task.Run(() => client.PostAsync(client.BaseAddress + "/user/create", postContent));
task.Wait();https://stackoverflow.com/questions/67712129
复制相似问题