HttpClient .NET 4.5 到 .NET 4.0 的降级代码 [英] Downgrade code of HttpClient .NET 4.5 to .NET 4.0

查看:29
本文介绍了HttpClient .NET 4.5 到 .NET 4.0 的降级代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的这段代码在 .NET 4.5 中运行良好.

I have this code that is working fine in .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 中做同样的事情.

Now I have to do the same thing in .NET 4.0.

但是我面临两个问题,我不知道如何解决.

But I am facing two problems I do not know how to resolve them.

  1. 在 .NET 4.0 中.方法 client.PostAsJsonAsync 不存在.
  2. 现有方法是client.PostAsync,它需要HttpContext.
  1. In .NET 4.0. method client.PostAsJsonAsync does not exist.
  2. The existing method is client.PostAsync and it needs HttpContext.

我确实在 WPF 客户端中请求...伙计们,我不知道我可以做些什么来存档相同的功能...

I do request within WPF client... Guys, I have no clue what I can do to archive the same functionality...

请帮忙!

推荐答案

建议使用 BCL/async/Microsoft HTTP Client Libraries" 帮助项目来补充" .Net 4.0 与 .Net 4.5 的等效功能(可以找到NuGet 包管理器中的最新版本.)有关详细信息,请参阅以下链接:http://www.nuget.org/packages/microsoft.bcl.async(注意:您可以通过相同的基本机制获得对 http 客户端的支持)

Suggest using the BCL / async / "Microsoft HTTP Client Libraries" helper projects to "supplement" .Net 4.0 with equivalent functionality to .Net 4.5 (Can find the latest versions in the NuGet package manager.) See the following link for more info: http://www.nuget.org/packages/microsoft.bcl.async (Note: you can get support for http client via same basic mechanism)

这篇关于HttpClient .NET 4.5 到 .NET 4.0 的降级代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆