我怎么能这样.NET RestSharp代码转换为Microsoft.Net.Http HttpClient的代码? [英] How can I convert this .NET RestSharp code to Microsoft.Net.Http HttpClient code?
本文介绍了我怎么能这样.NET RestSharp代码转换为Microsoft.Net.Http HttpClient的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想弄清楚如何使用的HttpClient
到 POST
一些简单的参数。
I'm trying to figure out how to use HttpClient
to POST
some simple parameters.
- 电子邮件
- 密码
我一直在RestSharp这样做,但我试图迁移关闭该
I've been doing this with RestSharp, but I'm trying to migrate off that.
我怎样才能做到这一点与的HttpClient
吗?
How can I do this with HttpClient
, please?
我有以下RestSharp代码
I have the following RestSharp code
var restRequest = new RestRequest("account/authenticate", Method.POST);
restRequest.AddParameter("Email", email);
restRequest.AddParameter("Password", password);
我如何可以转换到使用(Microsoft.Net.Http) HttpClient的
类,而不是
记:我在做一个POST
Take note: I'm doing a POST
此外,这是与PCL组装。
Also, this is with the PCL assembly.
最后,我可以在自定义头添加。你说:ILikeTurtles,真正的
Lastly, can I add in a custom header. Say: "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));
这篇关于我怎么能这样.NET RestSharp代码转换为Microsoft.Net.Http HttpClient的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文