简体   繁体   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

I'm using ModernHttpClient library and I'm to trying to get the Cookies from the httpClient's response 我正在使用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;

}

But the below line is giving error: 但是以下行给出错误:

IEnumerable<Cookie> responseCookies = cookieHandler.Cookies;

Error: 错误:

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

I've this code in a PCL. 我把这个代码放在PCL中。 Why am I getting this error? 为什么我收到此错误? How do I get the cookies correctly? 如何正确获取cookie?

EDIT: 编辑:

Now I've modernhttpclient library in both my pcl and ios project to make it work. 现在我在我的pcl和ios项目中都使用了modernhttpclient库来使其工作。 If I remove it from the ios project. 如果我从ios项目中删除它。 I get the same error. 我犯了同样的错误。

When you added the reference to the solution did you add the reference to the PCL project as well as the iOS and Android projects? 当您添加对解决方案的引用时,您是否添加了对PCL项目以及iOS和Android项目的引用?

It's the same Nuget package, but the android package adds a reference to 它是相同的Nuget包,但是android包添加了一个引用

~\\packages\\modernhttpclient.2.4.2\\lib\\MonoAndroid 〜\\包\\ modernhttpclient.2.4.2 \\ LIB \\ MonoAndroid

iOS gets a reference to iOS获取引用

~\\packages\\modernhttpclient.2.4.2\\lib\\Xamarin.iOS10 〜\\包\\ modernhttpclient.2.4.2 \\ LIB \\ Xamarin.iOS10

I'm assuming this is in Visual Studio? 我假设这是在Visual Studio中? Perhaps just uninstall the package and reinstall it (and install it at the solution level). 也许只需卸载软件包并重新安装(并在解决方案级别安装)。

Make sure your projects have correct references to assemblies: 确保您的项目具有对程序集的正确引用:

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

If that doesn't help, I suggest to stop using of the ModernHttpClient , due to the next: 如果这没有帮助,我建议停止使用ModernHttpClient ,因为下一个:

  1. Xamarin.iOS provides functionality of the ModernHttpClient out of the box (a bit limited though). Xamarin.iOS 提供ModernHttpClient的功能开箱(有点有限虽然)的。
  2. Actually, the ModernHttpClient.NativeMessageHandler does nothing with the cookieHandler parameter. 实际上, ModernHttpClient.NativeMessageHandlercookieHandler参数没有任何cookieHandler You can check it in the source code ( Android , iOS ). 您可以在源代码( AndroidiOS )中查看它。 And you can just handle cookies natively instead of using of ModernHttpClient implementations, see the source code for examples ( Android , iOS ). 您可以原生地处理cookie而不是使用ModernHttpClient实现,请参阅示例的源代码( AndroidiOS )。

Some more clarifications, as explained by Eugene and HeisenBerg. 如Eugene和HeisenBerg所解释的更多澄清。 You were trapped by " Bait and Switch " 你被“ 诱饵和开关 ”困住了

For future googlers: it may happen when you update some NuGet reference (eg modernhttpclient-updated ) to a version which targets Xamarin.Android 8.1 (just for example) while your app targets Xamarin.Android 6.0 : in that case your Xamarin.Android project wouldn't reference platform-specific code but only shared . 对于未来的googlers:当您将某些NuGet引用(例如modernhttpclient-updatedmodernhttpclient-updated为针对Xamarin.Android 8.1 (仅作为示例)的版本时,可能会发生这种情况,而您的应用程序针对Xamarin.Android 6.0 :在这种情况下您的Xamarin.Android项目不会引用特定于平台的代码,而只是共享代码。

Pay attention to NuGet warning! 注意NuGet警告!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将现有的C#项目导入可移植类库,并使用它引用Visual Studio中的android应用? - How to import your existing C# project into a portable class library and use it to reference to android app in visual studios? 您的 SQL 语法有错误; 在 C# 中查看与您的 MySQL 服务器版本相对应的手册 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version in C# C# 中的 MySQL 错误 - 您的 SQL 语法中有错误; 检查与您的 MySQL 服务器版本相对应的手册以获取正确的语法 - MySQL Error In C# - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax 可移植类库-您必须添加对程序集&#39;System.Net,版本= 2.0.5.0的引用 - Portable Class Library - you must add a reference to assembly 'System.Net, Version=2.0.5.0 您必须添加对程序集&#39;mscorlib,Version = 2.0.5.0 ...&#39;的引用C#WP7 XNA - You must add a reference to assembly 'mscorlib, Version=2.0.5.0…' C# WP7 XNA C# 和数据结构,您是否需要自己编写? - C# and datastructures, do you ever need to write your own? 您可以为 C# 版本添加预处理器指令吗? - Can you add a preprocessor directive for a C# version? C#参考任何版本? - C# Reference any version? 如何在C#Xamarin iOS项目中添加ios框架? - how do you add an ios framework to your C# Xamarin iOS project? 在 C# 中如何通过引用传递? - How do you pass by reference in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM