我有一份在.NET 4.5中运行良好的代码。
var handler = new HttpClientHandler();
handler.UseDefaultCredentials = true;
handler.PreAuthenticate = true;
handler.ClientCertificateOptions = ClientCertificateOption.Automatic;
var client = new HttpClient(handler);
client.BaseAddress = new Uri("http://localhost:22678/");
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
var loginBindingModel = new LoginBindingModel { Password = "test01", UserName = "test01" };
var response = await client.PostAsJsonAsync("api/Account/Login", loginBindingModel);
response.EnsureSuccessStatusCode(); // Throw on error code.
tokenModel = await response.Content.ReadAsAsync<TokenModel>();
现在我需要在.NET 4.0中做同样的事情。
但是我面临两个问题,我不知道如何解决它们。
- 在.NET 4.0中,方法
client.PostAsJsonAsync
不存在。 - 现有的方法是
client.PostAsync
,它需要HttpContext
。
我在WPF客户端内进行请求...伙计们,我不知道我该怎么做才能实现相同的功能...
请帮忙!