问题
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>();
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.
- In .NET 4.0. method
client.PostAsJsonAsyncdoes not exist. - The existing method is
client.PostAsyncand it needsHttpContext.
I do request within WPF client... Guys, I have no clue what I can do to archive the same functionality...
Please, help!
回答1:
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)
回答2:
Just for someone who is looking for the same .NET 4.0 pure solution I found that code working fine. But It does not have ASYNC/AWAIT thing that should be implemented as well. Anyway it is working exactly the same way as my code in the question.
var webAddr = "http://localhost:22678/api/Account/Login";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
httpWebRequest.ContentType = "application/json; charset=utf-8";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
var loginBindingModel = new WebAPILoginBindingModel { Password = "test01", UserName = "test01" };
var myJsonString = Newtonsoft.Json.JsonConvert.SerializeObject(loginBindingModel);
streamWriter.Write(myJsonString);
streamWriter.Flush();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
If you want to use use ASYNC/AWAIT for .NET 4.0 you have to install Microsoft.BCL dll and use async versions of HttpClient as well.
回答3:
Even after installing the following packages i got other errors.
Install-Package Microsoft.Net.Http
Install-Package System.Net.Http.Formatting.Extension
Turns out Install-Package System.Net.Http.Formatting.Extension installs version of system.net that does not support dot net 4. So I has to stick to PostAsync and create an httpContent object from my custom object.
So
PostAsJsonAsync("api/Account/Login", loginBindingModel)
Becomes (Note i used json.net here )
public HttpContent CreateHttpContent(object data)
{
return new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");
}
PostAsync("api/Account/Login",CreateHttpContent(loginBindingModel))
来源:https://stackoverflow.com/questions/21266807/downgrade-code-of-httpclient-net-4-5-to-net-4-0