Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My codes is here,It run well in my localhost,but after publish it is wrong .I catch the exception,It says that have a error called " System.Threading.Tasks.Task.Wait Method (Int32, CancellationToken) in System.Threading.Tasks.~1.get_Result()",How to fix the code?My .net framework is 4.0,not 4.5.I find the postAsyc with .Result can cause deathlock from the web,but how to avoid it?
C#
try
 {
  string ip="192.165.0.6";//in fact,it is read from database
  HttpClient httpClient = new HttpClient() { BaseAddress = new Uri("http://"+ip+"/User") };
  var checkParams = new Dictionary<string, string>() {
  { "UserName","Lyly"},
  { "Password", "123456"}
  };
 var validResponse = httpClient.PostAsync("http://"+ip+"/User/CheckUserValid", new FormUrlEncodedContent(checkParams)).Result;
  if (validResponse.StatusCode == HttpStatusCode.OK && validResponse.Content.ReadAsStringAsync().Result.Contains("ValidSuccess"))
  {
    var PointData= new Dictionary<string, string>() {
   { "Math","86"},
     { "English", "90"}, 
   };

 var getResultResponse = httpClient.PostAsync("http://"+ip+"/User/SendPointData", new FormUrlEncodedContent(PointData)).Result;
  if (getResultResponse.StatusCode == HttpStatusCode.OK)
    {
      if (getResultResponse.Content.ReadAsStreamAsync().Result != null)
      {
      var reader = new StreamReader(getResultResponse.Content.ReadAsStreamAsync().Result);
      var lastResutl= JsonHelper.FromJson<LastRest>(reader.ReadToEnd());
      }
    else return "Status1";
    }
   else return "Status2";
 }
else return "Status3";
}
catch(Exception e){
return e.StackTrace;
 }
Posted
Updated 11-Oct-13 15:01pm
v4

1 solution

According to this page[^] HttpClient.PostAsync is not supported in .NET 4 (only 4.5).

In fact, the entire HttpClient doesn't exist in 4.0. So I'm not sure how you are running it on 4.

Edit:

Unless you are using this library[^]?
 
Share this answer
 
v2
Comments
PEIYANGXINQU 11-Oct-13 15:35pm    
I use this httpclient,and it actually has HttpClient.PostAsync() function.
#region System.Net.Http.dll, v2.0.0.0
// C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies\System.Net.Http.dll
#endregion

using System;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;

namespace System.Net.Http
{
// Provides a base class for sending HTTP requests and receiving HTTP responses
// from a resource identified by a URI.
public class HttpClient : HttpMessageInvoker
Ron Beyer 11-Oct-13 15:37pm    
So you took a .NET 4.5 assembly and linked it to a .NET 4 assembly?
PEIYANGXINQU 11-Oct-13 15:50pm    
My whole project framework is 4.0.May be the System.Net.Http.dll is 4.5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900