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

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

问题描述

我有这个code,它是工作的罚款在.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 /异步/微软HTTP客户端库的辅助项目,以补充.NET 4.0中具有同等功能的.NET 4.5(能找到的最新版本中的NuGet包管理器)。 请参阅下面的链接了解更多信息:<一href="http://www.nuget.org/packages/microsoft.bcl.async">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的降级code到.NET 4.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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