我正在使用 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包添加了对
的引用~\packages\modernhttpclient.2.4.2\lib\MonoAndroid
iOS 获取对
的引用~\packages\modernhttpclient.2.4.2\lib\Xamarin.iOS10
我假设这是在 Visual Studio 中?也许只需卸载该软件包并重新安装(并在解决方案级别安装)。
关于C#: ModernHttpClient,你在你的应用程序中引用了可移植版本——你需要引用平台 (iOS/Android) 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37917209/
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
我注意到像bundler这样的项目在每个specfile中执行requirespec_helper我还注意到rspec使用选项--require,它允许您在引导rspec时要求一个文件。您还可以将其添加到.rspec文件中,因此只要您运行不带参数的rspec就会添加它。使用上述方法有什么缺点可以解释为什么像bundler这样的项目选择在每个规范文件中都需要spec_helper吗? 最佳答案 我不在Bundler上工作,所以我不能直接谈论他们的做法。并非所有项目都checkin.rspec文件。原因是这个文件,通常按照当前的惯例,只
我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘
我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121
我正在尝试修改当前依赖于定义为activeresource的gem:s.add_dependency"activeresource","~>3.0"为了让gem与Rails4一起工作,我需要扩展依赖关系以与activeresource的版本3或4一起工作。我不想简单地添加以下内容,因为它可能会在以后引起问题:s.add_dependency"activeresource",">=3.0"有没有办法指定可接受版本的列表?~>3.0还是~>4.0? 最佳答案 根据thedocumentation,如果你想要3到4之间的所有版本,你可以这
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
如果我使用ruby版本2.5.1和Rails版本2.3.18会怎样?我有基于rails2.3.18和ruby1.9.2p320构建的rails应用程序,我只想升级ruby的版本,而不是rails,这可能吗?我必须面对哪些挑战? 最佳答案 GitHub维护apublicfork它有针对旧Rails版本的分支,有各种变化,它们一直在运行。有一段时间,他们在较新的Ruby版本上运行较旧的Rails版本,而不是最初支持的版本,因此您可能会发现一些关于需要向后移植的有用提示。不过,他们现在已经有几年没有使用2.3了,所以充其量只能让更
我安装了ruby版本管理器,并将RVM安装的ruby实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby。有没有办法让emacs像shell一样尊重ruby的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的