使用netstandard 2的xamarin表单中的简单http请求

我有十几个访问api后端的应用程序,但最近我正在玩netstandard 2的空白xamarin表单,我不能使用hhtpclient访问后端....在两台机器上试过

var uri = "https://myurl";

                var handler = new HttpClientHandler
                {
                    Credentials = new NetworkCredential("user", "P@ssw0rd"),
                    UseDefaultCredentials = false
                };

                var formContent = new FormUrlEncodedContent(new[]
                {
                new KeyValuePair<string, string>("MATRICULA", "2147483647")
            });

                var client = new HttpClient(handler);
                var response = await client.PostAsync(uri, formContent);
                var text = await response.Content.ReadAsStringAsync();

异常os postasync行

{System.Net.Http.HttpRequestException:发送请求时发生错误---> System.Net.WebException:获取响应流(ReadDone2)时出错:ReceiveFailure ---> System.Exception:在System.Net.WebConnection . HandleError(System.Net.WebExceptionStatus st,System.Exception e,System.String where)[0x00031] in:0,System.Net.WebConnection.ReadDone(System.IAsyncResult result)[0x00056] in:0,System.Threading . Tasks.TaskToApm <> c__DisplayClass3_0.b__0()[0x00000] in:System.Shreading.Tasks.AwaitTaskContinuation.InvokeAction(System.Object state)[0x00000] in:0 in System.Threading.ExecutionContext.RunInternal(System.Threading) .ExecutionContext executionContext,System.Threading.ContextCallback callback,System.Object state,System.Boolean preserveSyncCtx)[0x00071] in:0 at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback callback, System.Object state,System.Boolean preserveSyncCtx)[0x00000 ] in:System.Shreading.Tasks.AwaitTaskContinuation.RunCallback(System.Threading.ContextCallback回调,System.Object状态,System.Threading.Tasks.Task&currentTask)中的0:[0x0001a] in:0,System.Threading.Tasks.AwaitTaskContinuation .Run(System.Threading.Tasks.Task任务,System.Boolean canInlineContinuationTask)[0x0000a] in:0,System.Threading.Tasks.Task.FinishContinuations()[0x000b1] in:0,System.Threading.Tasks.Task . FinishStageThree()[0x0003c] in:0 at System.Threading.Tasks.Task 1[TResult].TrySetResult (TResult result) [0x0004f] in <cf9013b38a4e4129bd64785080dd2844>:0 at System.Runtime.CompilerServices.AsyncTaskMethodBuilder 1 [TResult] .SetResult(TResult result)[0x0003e] in:0 at Mono.Net.Security.MobileAuthenticatedStream d__58.MoveNext()[0x001ee] in:0 at System.Runtime.CompilerServices.AsyncMethodBuilderCore MoveNextRunner.InvokeMoveNext(System.Object stateMachine)[0x00000] in:0 at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback callback,System .Object state,System.Boolean preserveSyncCtx)[0x00071] in:0 at S ystem.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback callback,System.Object state,System.Boolean preserveSyncCtx)[0x00000] in:0 at System.Runtime.CompilerServices.AsyncMethodBuilderCore MoveNextRunner.Run( )[0x00024] in:0:System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action,System.Boolean allowInlining,System.Threading.Tasks.Task&currentTask)[0x0001c] in:0 in System.Threading.Tasks . Task.FinishContinuations()[0x00068] in:0 in System.Threading.Tasks.Task.FinishStageThree()[0x0003c] in:0 at System.Threading.Tasks.Task 1[TResult].TrySetResult (TResult result) [0x00000] in <cf9013b38a4e4129bd64785080dd2844>:0 at System.Runtime.CompilerServices.AsyncTaskMethodBuilder 1 [TResult] .SetResult(TResult result)[0x00000 ] in:0 at Mono.Net.Security.AsyncProtocolRequest d__23.MoveNext()[0x000dd] in:0 in System.Runtime.CompilerServices.AsyncMethodBuilderCore MoveNextRunner.InvokeMoveNext(System.Object stateMachine)[0x00000] in:0 at System.Threading .ExecutionContext.RunInternal(System.Threading.ExecutionContex t executionContext,System.Threading.ContextCallback回调,System.Object状态,System.Boolean preserveSyncCtx)[0x00071] in:0 at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback callback,System .Object state,System.Boolean preserveSyncCtx)[0x00000] in:0 in System.Runtime.CompilerServices.AsyncMethodBuilderCore MoveNextRunner.Run()[0x00024] in:0 at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action, System.Toolean allowInlining,System.Threading.Tasks.Task&currentTask)[0x0001c] in:0 in System.Threading.Tasks.Task.FinishContinuations()[0x00068] in:0 at System.Threading.Tasks.Task.FinishStageThree() [0x0003c] in:0 at System.Threading.Tasks.Task 1[TResult].TrySetResult (TResult result) [0x0004f] in <cf9013b38a4e4129bd64785080dd2844>:0 at System.Runtime.CompilerServices.AsyncTaskMethodBuilder 1 [TResult] .SetResult(TResult result)[0x0003e] in:0 at System.Runtime.CompilerServices.AsyncTaskMethodBuilder 1[TResult].SetResult (System.Threading.Tasks.Task 1 [TResult] completedTask)[0x00010] in :System.Runtime.CompilerServices.AsyncTaskM:0 ethodBuilder.SetResult()[0x00000] in:0 at Mono.Net.Security.AsyncProtocolRequest d__24.MoveNext()[0x001d8] in:0 atSystem.Runtime.CompilerServices.AsyncMethodBuilderCore MoveNextRunner.InvokeMoveNext(System.Object stateMachine)[0x00000] in:0 at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback callback,System.Object state, System.Boolean preserveSyncCtx)[0x00071] in:0 in System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback callback,System.Object state,System.Boolean preserveSyncCtx)[0x00000] in:0在System.Runtime.CompilerServices.AsyncMethodBuilderCore MoveNextRunner.Run()[0x00024] in:0 in System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action,System.Boolean allowInlining,System.Threading.Tasks.Task&currentTask)[ 0x0001c] in:0,System.Shreading.Tasks.Task.FinishContinuations()[0x00068] in:0,System.Threading.Tasks.Task.FinishStageThree()[0x0003c] in:0,System.Threading.Tasks.Task 130024 5 1 [TResult] .SetResult(TResult result)[0x0003e] in:0 at Mono.Net.Security.AsyncProtocolRequest d__25.MoveNext()[0x00169] in:0 at System.Runtime.CompilerServices.AsyncMethodBuilderCore MoveNextRunner.InvokeMoveNext(System . 对象stateMachine)[0x00000] in:0表示System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback回调,System.Object状态,System.Boolean preserveSyncCtx)[0x00071] in:0 at System .Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback callback,System.Object state,System.Boolean preserveSyncCtx)[0x00000] in:0 at System.Runtime.CompilerServices.AsyncMethodBuilderCore MoveNextRunner.Run() [0x00024] in:0:System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action,System.Boolean allowInlining,System.Threading.Tasks.Task&currentTask)[0x0001c] in:0 at System.Threading.Tasks.Task .FinishCo ntinuations()[0x00068] in:0,System.Shreading.Tasks.Task.FinishStageThree()[0x0003c] in:0 at System.Threading.Tasks.Task 1[TResult].TrySetResult (TResult result) [0x0004f] in <cf9013b38a4e4129bd64785080dd2844>:0 at System.Runtime.CompilerServices.AsyncTaskMethodBuilder 1 [TResult] .SetResult(TResult result)[0x0003e] in :0在Mono.Net.Security.MobileAuthenticatedStream d__66.MoveNext()[0x0017c] in:0 in System.Runtime.CompilerServices.AsyncMethodBuilderCore MoveNextRunner.InvokeMoveNext(System.Object stateMachine)[0x00000] in:0 at System.Threading.ExecutionContext .RunInternal(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback callback,System.Object state,System.Boolean preserveSyncCtx)[0x00071] in:0 at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext,System .Threading.ContextCallback回调,System.Object状态,System.Boolean preserveSyncCtx)[0x00000] in:0 in System.Runtime.CompilerServices.AsyncMethodBuilderCore MoveNextRunner.Run()[0x00024] in:0 at System.Threading.Tasks.AwaitTaskContinuation . RunOrScheduleAction(System.A ction action,System.Boolean allowInlining,System.Threading.Tasks.Task&currentTask)[0x0001c] in:0 at System.Threading.Tasks.Task.FinishContinuations()[0x00068] in:0 at System.Threading.Tasks.Task . FinishStageThree()[0x0003c] in:0 at System.Threading.Tasks.Task 1[TResult].TrySetResult (TResult result) [0x0004f] in <cf9013b38a4e4129bd64785080dd2844>:0 at System.Threading.Tasks.TaskFactory 1 FromAsyncTrimPromise 1[TResult,TInstance].Complete (TInstance thisRef, System.Func 3 [T1,T2,TResult] endMethod,System.IAsyncResult asyncResult,System.Boolean requiresSynchronization)[0x0000d] in:0 at System . Threading.Tasks.TaskFactory 1+FromAsyncTrimPromise 1 [TResult,TInstance] .CompleteFromAsyncResult(System.IAsyncResult asyncResult)[0x00073] in:0 at System.Net.Sockets.SocketAsyncResult <> c.b__27_0(System.Object state)[0x00000] in: 0在System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()[0x00015] in:0 at System.Threading.ThreadPoolWorkQueue.Dispatch()[0x00074] in:0 at ObjCRuntime.Runtime.ThreadPoolDispatcher(System.Func 1[TResult] callback) [0x00006] in <b7935acd70e343049845d6fd73e5ec44>:0 at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () [0x00009] in <cf9013b38a4e4129bd64785080dd2844>:0 at System.Net.WebConnection.HandleError (System.Net.WebExceptionStatus st, System.Exception e, System.String where) [0x00031] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.8.0.20/src/Xamarin.iOS/mcs/class/System/System.Net/WebConnection.cs:439 --- End of inner exception stack trace --- at System.Net.HttpWebRequest.EndGetResponse (System.IAsyncResult asyncResult) [0x00059] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.8.0.20/src/Xamarin.iOS/mcs/class/System/System.Net/HttpWebRequest.cs:1033 at System.Threading.Tasks.TaskFactory 1 [TResult] .FromAsyncCoreLogic(System.IAsyncResult iar,System.Func 2[T,TResult] endFunction, System.Action 1 [T] e ndAction,System.Threading.Tasks.Task 1[TResult] promise, System.Boolean requiresSynchronization) [0x0000f] in <cf9013b38a4e4129bd64785080dd2844>:0 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.8.0.20/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:152 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00037] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.8.0.20/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.8.0.20/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.8.0.20/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 at System.Runtime.CompilerServices.ConfiguredTaskAwaitable 1 ConfiguredTaskAwaiter [TResult] .GetResult()[0x00000] in:0,位于/Library/Frameworks/Xamarin.iOS中的System.Net.Http.HttpClientHandler d__64.MoveNext()[0x003d3] .framework / Versions / 11.8.0.20 / src / Xamarin.iOS / mcs / class / System.Net.Http / System.Net.Http / HttpClientHandler.cs:402 ---内部异常堆栈跟踪结束---在系统.Net.Http.HttpClientHandler d__64.MoveNext()[0x0046c] in/Library/Frameworks/Xamarin.iOS.framework/Versions/11.8.0.20/src/Xamarin.iOS/mcs/class/System.Net.Http/System.Net.Http/HttpClientHandler.cs:406 ---堆栈结束从抛出异常的先前位置追踪---在/Library/Frameworks/Xamarin.iOS.framework/Versions/11.8.0.20/src/Xamarin.iOS/中的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()[0x0000c]在/ Library / Frameworks / Xamarin中的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task任务)[0x00037]中的mcs / class / referencesource / mscorlib / system / runtime / exceptionservices / exceptionservicescommon.cs:152 . iOS.framework / Versions / 11.8.0.20 / src / Xamarin.iOS / mcs / class / referencesource / mscorlib / system / runtime / compilerservices / TaskAwaiter.cs:187 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading . 在/Library/Frameworks/Xamarin.iOS.framework/Versions/11.8.0.20/src/Xamarin.iOS/mcs/class/referencesource/mscorlib中的Tasks.Task任务)[0x00028] /system/runtime/compilerservices/TaskAwaiter.cs:156 at /Library/Frameworks/Xamarin.iOS.framework/Versions/11.8中的System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(System.Threading.Tasks.Task任务)[0x00008] .0.20 / src / Xamarin.iOS / mcs / class / referencesource / mscorlib / system / runtime / compilerservices / TaskAwaiter.cs:128 at System.Runtime.CompilerServices.ConfiguredTaskAwaitable 1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in <cf9013b38a4e4129bd64785080dd2844>:0 at System.Net.Http.HttpClient+<SendAsyncWorker>d__48.MoveNext () [0x00080] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.8.0.20/src/Xamarin.iOS/mcs/class/System.Net.Http/System.Net.Http/HttpClient.cs:276 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.8.0.20/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:152 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00037] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.8.0.20/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.8.0.20/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.8.0.20/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 at System.Runtime.CompilerServices.TaskAwaiter 1 [TResult] .GetResult()[0x00000] in: 0在App6.MainPage d _1.MoveNext()[0x000b0]在C:\ Users \ gilbe \ source \ repos \ App6 \ App6 \ App6 \ MainPage.xaml.cs:38}

回答(1)

3 years ago

转到Android visual studio项目属性 . 然后转到Android选项,然后进入高级(在底部),然后执行HttpClient并从列表中选择Android .