C#:ModernHttpClient,您在您的应用程序中引用便携式版本 - 您需要引用平台(iOS/Android)版本
Posted
技术标签:
【中文标题】C#:ModernHttpClient,您在您的应用程序中引用便携式版本 - 您需要引用平台(iOS/Android)版本【英文标题】:C#: ModernHttpClient, You're referencing the Portable version in your App - you need to reference the platform (iOS/Android) version 【发布时间】:2016-06-20 07:54:59 【问题描述】:我正在使用 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 项目中删除它。我得到同样的错误。
【问题讨论】:
【参考方案1】:当您添加对解决方案的引用时,您是否添加了对 PCL 项目以及 iOS 和 Android 项目的引用?
是同一个Nuget包,只是android包增加了对
的引用~\packages\modernhttpclient.2.4.2\lib\MonoAndroid
iOS 获得对
的引用~\packages\modernhttpclient.2.4.2\lib\Xamarin.iOS10
我假设这是在 Visual Studio 中?也许只是卸载软件包并重新安装它(并在解决方案级别安装它)。
【讨论】:
我正在使用 Xamarin Studio 我仍然认为这是同一个问题 - 那里的包管理器应该能够为每个平台下载正确的包。【参考方案2】:对于未来的谷歌员工:当您将一些 NuGet 引用(例如 modernhttpclient-updated
)更新为以 Xamarin.Android 8.1
为目标的版本时(例如)而您的应用程序以 Xamarin.Android 6.0
为目标时,可能会发生这种情况:在这种情况下,您的 Xamarin.Android项目不会引用特定于平台的代码,而只会引用共享。
注意 NuGet 警告!
【讨论】:
modernhttpclient-updated 的哪个版本针对哪个 xamarin android 版本?【参考方案3】:确保您的项目对程序集有正确的引用:
Android:在MonoAndroid
文件夹中
iOS:在Xamarin.iOS10
文件夹(或MonoTouch
文件夹)中
PCL:在Portable-Net45+WinRT45+WP8+WPA81
文件夹中
如果这没有帮助,我建议停止使用 ModernHttpClient,因为下一个:
-
ModernHttpClient 的 Xamarin.iOS provides 功能开箱即用(虽然有点受限)。
实际上,
ModernHttpClient.NativeMessageHandler
对 cookieHandler
参数没有任何作用。您可以在源代码中查看它(Android,iOS)。您可以直接处理 cookie,而不是使用 ModernHttpClient 实现,请参阅源代码示例(Android、iOS)。
【讨论】:
我宁愿建议使用modernhttpclient-updated
,因为 Xamarin 的 HttpClientHandler 不适用于 Android 4.x。【参考方案4】:
Eugene 和 HeisenBerg 解释了更多说明。 你被“Bait and Switch”困住了
【讨论】:
以上是关于C#:ModernHttpClient,您在您的应用程序中引用便携式版本 - 您需要引用平台(iOS/Android)版本的主要内容,如果未能解决你的问题,请参考以下文章
您在您的网站中使用啥 Unicode 字符? (而不是图像图标)
是否有可能在您的应用程序中收到您在消息应用程序中收到的相同消息?
是否有可能在您的应用程序中收到您在消息应用程序中收到的相同消息?