簡體   English   中英

C#:ModernHttpClient,您在應用程序中引用了Portable版本 - 您需要引用平台(iOS / Android)版本

[英]C#: ModernHttpClient, You're referencing the Portable version in your App - you need to reference the platform (iOS/Android) version

我正在使用ModernHttpClient庫,我試圖從httpClient的響應中獲取Cookie

public static async Task<String> loginUser()
{
    var values = new List<KeyValuePair<string, string>>
    {
        new KeyValuePair<string, string>("username", "*****"),
        new KeyValuePair<string, string>("password", "*****"),
    };
    NativeCookieHandler cookieHandler = new NativeCookieHandler();
    NativeMessageHandler messageHandler = new NativeMessageHandler(false, false, cookieHandler);
    var httpClient = new HttpClient(messageHandler);

    var response = await httpClient.PostAsync(RestApiPaths.LOGIN, new FormUrlEncodedContent(values));

    response.EnsureSuccessStatusCode();
    String resultString = await response.Content.ReadAsStringAsync();
    System.Diagnostics.Debug.WriteLine("resultString: " + resultString);
    IEnumerable<Cookie> responseCookies = cookieHandler.Cookies;
    Cookie mCookie = responseCookies.FirstOrDefault();
    RestApiPaths.mCookie = mCookie;
    return resultString;

}

但是以下行給出錯誤:

IEnumerable<Cookie> responseCookies = cookieHandler.Cookies;

錯誤:

MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] System.Exception: You're referencing the Portable version in your App - you need to reference the platform (iOS/Android) version
[MonoDroid]   at Gogch.MyClass+<loginUser>c__async0.MoveNext () <0x9e0d9e08 + 0x004b8> in <filename unknown>:0 
[MonoDroid] --- End of stack trace from previous location where exception was thrown ---
[MonoDroid]   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <0x9d3a3670 + 0x00024> in <filename unknown>:0 
[MonoDroid]   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) <0x9d3a2ff8 + 0x000bb> in <filename unknown>:0 
[MonoDroid]   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) <0x9d3a2dd0 + 0x0008b> in <filename unknown>:0 
[MonoDroid]   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) <0x9d6ecaa8 + 0x0003f> in <filename unknown>:0 
[MonoDroid]   at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () <0xa1f0fa18 + 0x0001b> in <filename unknown>:0 
[MonoDroid]   at Gogch.Droid.MainActivity+<button_OnClick>c__async0.MoveNext () <0xaf58c608 + 0x00137> in <filename unknown>:0 
[MonoDroid] --- End of stack trace from previous location where exception was thrown ---
[MonoDroid]   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <0x9d3a3670 + 0x00024> in <filename unknown>:0 
[MonoDroid]   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>m__0 (System.Object state) <0x9d3a4488 + 0x0003f> in <filename unknown>:0 
[MonoDroid]   at Android.App.SyncContext+<Post>c__AnonStorey0.<>m__0 () <0xa1f0f968 + 0x00027> in <filename unknown>:0 
[MonoDroid]   at Java.Lang.Thread+RunnableImplementor.Run () <0xa1f0f7b8 + 0x0003f> in <filename unknown>:0 
[MonoDroid]   at Java.Lang.IRunnableInvoker.n_Run (IntPtr jnienv, IntPtr native__this) <0xa1f0f710 + 0x0003f> in <filename unknown>:0 
[MonoDroid]   at (wrapper dynamic-method) System.Object:ba036dc5-deef-4850-b986-e1e85c84e6de (intptr,intptr)
[art] JNI RegisterNativeMethods: attempt to register 0 native methods for android.runtime.JavaProxyThrowable
[AndroidRuntime] Shutting down VM
[AndroidRuntime] FATAL EXCEPTION: main
[AndroidRuntime] Process: com.cinch.gogch, PID: 18910
[AndroidRuntime] java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
[AndroidRuntime]    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
[AndroidRuntime] Caused by: java.lang.reflect.InvocationTargetException
[AndroidRuntime]    at java.lang.reflect.Method.invoke(Native Method)
[AndroidRuntime]    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
[AndroidRuntime]    ... 1 more
[AndroidRuntime] Caused by: android.runtime.JavaProxyThrowable: System.Exception: You're referencing the Portable version in your App - you need to reference the platform (iOS/Android) version
[AndroidRuntime]   at Gogch.MyClass+<loginUser>c__async0.MoveNext () <0x9e0d9e08 + 0x004b8> in <filename unknown>:0 
[AndroidRuntime] --- End of stack trace from previous location where exception was thrown ---
[AndroidRuntime]   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <0x9d3a3670 + 0x00024> in <filename unknown>:0 
[AndroidRuntime]   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) <0x9d3a2ff8 + 0x000bb> in <filename unknown>:0 
[AndroidRuntime]   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) <0x9d3a2dd0 + 0x0008b> in <filename unknown>:0 
[AndroidRuntime]   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) <0x9d6ecaa8 + 0x0003f> in <filename unknown>:0 
[AndroidRuntime]   at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () <0xa1f0fa18 + 0x0001b> in <filename unknown>:0 
[AndroidRuntime]   at Gogch.Droid.MainActivity+<button_OnClick>c__async0.MoveNext () <0xaf58c608 + 0x00137> in <filename unknown>:0 
[AndroidRuntime] --- End of stack trace from previous location where exception was thrown ---
[AndroidRuntime]   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <0x9d3a3670 + 0x00024> in <filename unknown>:0 
[AndroidRuntime]   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>m__0 (System.Object state) <0x9d3a4488 + 0x0003f> in <filename unknown>:0 
[AndroidRuntime]   at Android.App.SyncContext+<Post>c__AnonStorey0.<>m__0 () <0xa1f0f968 + 0x00027> in <filename unknown>:0 
[AndroidRuntime]   at Java.Lang.Thread+RunnableImplementor.Run () <0xa1f0f7b8 + 0x0003f> in <filename unknown>:0 
[AndroidRuntime]   at Java.Lang.IRunnableInvoker.n_Run (IntPtr jnienv, IntPtr native__this) <0xa1f0f710 + 0x0003f> in <filename unknown>:0 
[AndroidRuntime]   at (wrapper dynamic-method) System.Object:ba036dc5-deef-4850-b986-e1e85c84e6de (intptr,intptr)
[AndroidRuntime]    at mono.java.lang.RunnableImplementor.n_run(Native Method)
[AndroidRuntime]    at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:30)
[AndroidRuntime]    at android.os.Handler.handleCallback(Handler.java:746)
[AndroidRuntime]    at android.os.Handler.dispatchMessage(Handler.java:95)
[AndroidRuntime]    at android.os.Looper.loop(Looper.java:148)
[AndroidRuntime]    at android.app.ActivityThread.main(ActivityThread.java:5443)
[AndroidRuntime]    ... 3 more

我把這個代碼放在PCL中。 為什么我收到此錯誤? 如何正確獲取cookie?

編輯:

現在我在我的pcl和ios項目中都使用了modernhttpclient庫來使其工作。 如果我從ios項目中刪除它。 我犯了同樣的錯誤。

當您添加對解決方案的引用時,您是否添加了對PCL項目以及iOS和Android項目的引用?

它是相同的Nuget包,但是android包添加了一個引用

〜\\包\\ modernhttpclient.2.4.2 \\ LIB \\ MonoAndroid

iOS獲取引用

〜\\包\\ modernhttpclient.2.4.2 \\ LIB \\ Xamarin.iOS10

我假設這是在Visual Studio中? 也許只需卸載軟件包並重新安裝(並在解決方案級別安裝)。

確保您的項目具有對程序集的正確引用:

  • Android:在MonoAndroid文件夾中
  • iOS:在Xamarin.iOS10文件夾(或MonoTouch文件夾)中
  • PCL:在Portable-Net45+WinRT45+WP8+WPA81文件夾中

如果這沒有幫助,我建議停止使用ModernHttpClient ,因為下一個:

  1. Xamarin.iOS 提供ModernHttpClient的功能開箱(有點有限雖然)的。
  2. 實際上, ModernHttpClient.NativeMessageHandlercookieHandler參數沒有任何cookieHandler 您可以在源代碼( AndroidiOS )中查看它。 您可以原生地處理cookie而不是使用ModernHttpClient實現,請參閱示例的源代碼( AndroidiOS )。

如Eugene和HeisenBerg所解釋的更多澄清。 你被“ 誘餌和開關 ”困住了

對於未來的googlers:當您將某些NuGet引用(例如modernhttpclient-updatedmodernhttpclient-updated為針對Xamarin.Android 8.1 (僅作為示例)的版本時,可能會發生這種情況,而您的應用程序針對Xamarin.Android 6.0 :在這種情況下您的Xamarin.Android項目不會引用特定於平台的代碼,而只是共享代碼。

注意NuGet警告!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM