我正在开发一个 WPF 应用程序,它可以使用 Http Client 与我的服务器通信。
我的应用程序建立在 .NET 4 上。我通过 nugget 安装 Http Client。我还 通过 nugget安装Microsoft.CompilerServices.AsyncTargetingPack 。
在 Windows 8 中它可以正常工作。但在 Windows 7 中我得到一个异常
应用程序:whost.Generator.Creator.exe 框架版本:v4.0.30319 描述:进程因未处理的异常而终止。异常信息:System.Net.Http.HttpRequestException 堆栈:
服务器堆栈跟踪:
在 [0] 处重新引发异常:在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at whost.Generator.Creator.Basic.ServerCommunicator.d__10.MoveNext()
在 [1] 处重新抛出异常:在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task) 在 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 的 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) whost.Generator.Creator.ViewModel.LicenseGenerateViewModel.d_ 3.MoveNext() 在 System.Runtime.CompilerServices.AsyncMethodBuilderCore.b_0(System.Object) 在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) 在 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate) 在 System.Windows.Threading.DispatcherOperation.InvokeImpl() 在 System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object) 在 System.Threading.ExecutionContext.runTryCode(System.Object) 在 System.Runtime .CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode, CleanupCode, System.Object) 在 System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) 在 System.Threading.ExecutionContext.Run(System .Threading.ExecutionContext,System.Threading。ContextCallback, System.Object, Boolean) 在 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) 在 System.Windows.Threading.DispatcherOperation.Invoke() 在 System.Windows。 Threading.Dispatcher.ProcessQueue() 在 System.Windows.Threading.Dispatcher.WndProcHook(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef) 在 MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef) 在MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object) 在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) 在 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object, System.委托,System.Object,Int32,System.Delegate)在 System.Windows.Threading.Dispatcher。在 MS.Win32.HwndSubclass.SubclassWndProc(IntPtr,Int32,IntPtr,IntPtr)的 InvokeImpl(System.Windows.Threading.DispatcherPriority,System.TimeSpan,System.Delegate,System.Object,Int32)
在 MS.Win32.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef) 在 System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame) 在 System.Windows.Threading.Dispatcher.PushFrame(System. Windows.Threading.DispatcherFrame) 在 System.Windows.Threading.Dispatcher.Run() 在 System.Windows.Application.RunDispatcher(System.Object) 在 System.Windows.Application.RunInternal(System.Windows.Window) 在 System.Windows .Application.Run(System.Windows.Window) 在 System.Windows.Application.Run() 在 whost.Generator.Creator.App.Main()
我不明白为什么我会得到这个异常。我的服务器建立在 MVC 4 和 WEB API 之上。
提前致谢。
更新 1:我的代码是:
var handler = new HttpClientHandler();
var cookieContainer = new CookieContainer();
handler.CookieContainer = cookieContainer;
var client = new HttpClient(handler) { BaseAddress = new Uri("http://myhost.com/") };
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
var loginModel = new LoginModel
{
UserName = Username,
Password = Password,
};
var userLoginResposnce = await client.PostAsJsonAsync("account/Login", loginModel);
var successInfoTamplate = new { Success = true };
var loginResponceString = await userLoginResposnce.Content.ReadAsStringAsync();
var isSuccess = JsonConvert.DeserializeAnonymousType(loginResponceString, successInfoTamplate);
我在我的 Windows 7 PC 上安装了 .NET 4。