我正在尝试弄清楚如何使用 HttpClient
来POST
一些简单的参数。
- 电子邮件
- 密码
我一直在使用 RestSharp 执行此操作,但我正在尝试迁移。
请问我如何使用 HttpClient
执行此操作?
我有以下 RestSharp 代码
var restRequest = new RestRequest("account/authenticate", Method.POST);
restRequest.AddParameter("Email", email);
restRequest.AddParameter("Password", password);
如何将其转换为使用 (Microsoft.Net.Http) HttpClient
类?
注意:我正在做一个 POST
此外,这是与 PCL 程序集一起使用的。
最后,我可以添加自定义 header 吗?说:"ILikeTurtles", "true"
。
最佳答案
应该这样做
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("ILikeTurtles", "true");
var parameters = new Dictionary<string, string>();
parameters["Email"] = "myemail";
parameters["Password"] = "password";
var result = await httpClient.PostAsync("http://www.example.com/", new FormUrlEncodedContent(parameters));
关于c# - 如何将此 .NET RestSharp 代码转换为 Microsoft.Net.Http HttpClient 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21805541/