简体   繁体   English

MvvmCross插件网络和Cookie

[英]MvvmCross Plugin Network and Cookie

I'm tring to make a rest request that require an embedded cookie. 我想提出一个需要嵌入式cookie的休息请求。 I've do that using System.Net reference in that way 我已经以这种方式使用System.Net参考来做到这一点

Reference to System.net on PCL and MVVMCross 在PCL和MVVMCross上对System.net的引用

but i've problem to port that PCL library (System.Net) on VS2012. 但是我在VS2012上移植该PCL库(System.Net)时遇到问题。 That code infact works fine on Xamarin Studio on Mac. 该代码实际上可以在Mac上的Xamarin Studio上正常运行。

Referred to that question i've tried to use Network Plugin of MVVMCross in that way: 提到这个问题,我尝试以这种方式使用MVVMCross的网络插件:

var url = "http://api.myaddress.com/api";
var json = new Cirrious.MvvmCross.Plugins.Json.MvxJsonConverter();
var client = new Cirrious.MvvmCross.Plugins.Network.Rest.MvxJsonRestClient
{
JsonConverterProvider = () => json
};
var request = new MvxRestRequest(url);
MvxDecodedRestResponse<SearchItem> theResponse = null;
    Exception exception = null;
CookieCollection cc = new CookieCollection ();
cc.Add(new Cookie("mycookiename","mycookievalue","/","api.myaddress.com"));

request.CookieContainer = new CookieContainer();

request.CookieContainer.Add(cc);
client.MakeRequestFor<SearchItem>(request,
                                                    (result) => { theResponse = result; },
                                                (error) => { exception = error; });

I've this error "The requested feature is not implemented." 我收到此错误“未实现请求的功能”。

at System.Net.HttpWebRequest.get_SupportsCookieContainer () [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:553 at (wrapper remoting-invoke-with-check) System.Net.HttpWebRequest:get_SupportsCookieContainer () at Cirrious.MvvmCross.Plugins.Network.Rest.MvxRestClient.SetCookieContainer (Cirrious.MvvmCross.Plugins.Network.Rest.MvxRestRequest restRequest, System.Net.HttpWebRequest httpRequest) [0x00000] in /Users/stuartlodge/Documents/GitHub/Mvx/MvvmCross/Plugins/Cirrious/Network/Cirrious.MvvmCross.Plugins.Network/Rest/MvxRestClient.cs:128 at Cirrious.MvvmCross.Plugins.Network.Rest.MvxRestClient.BuildHttpRequest (Cirrious.MvvmCross.Plugins.Network.Rest.MvxRestRequest restRequest) [0x00028] in /Users/stuartlodge/Documents/GitHub/Mvx/MvvmCross/Plugins/Cirrious/Network/Cirrious.MvvmCross.Plugins.Network/Rest/MvxRestClient.cs:100 at Cirrious.MvvmCross.Plugins.Network.Rest.MvxRestClient+c__AnonStorey2.<>m__4 () [0x0000d] in /Users/stuartlodge/Document 在/Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:553在(包装器远程调用并检查)系统的System.Net.HttpWebRequest.get_SupportsCookieContainer()[0x00000]中.Net.HttpWebRequest:get_SupportsCookieContainer()位于Cirrious.MvvmCross.Plugins.Network.Rest.MvxRestClient.SetCookieContainer(Cirrious.MvvmCross.Plugins.Network.Rest.MvxRestRequest restRequest,System.Net.HttpWebRequest httpRequest stuartlodge / Documents / GitHub / Mvx / MvvmCross / Plugins / Cirrious / Network / Cirrious.MvvmCross.Plugins.Network / Rest / MvxRestClient.cs:128在Cirrious.MvvmCross.Plugins.Network.Rest.MvxRestClient.Build。 /Users/stuartlodge/Documents/GitHub/Mvx/MvvmCross/Plugins/Cirrious/Network/Cirrious.MvvmCross.Plugins.Network/Rest/MvxRestClient.cs:100处/Users/stuartlodge/Documents/Plugins.Network.Rest.MvxRestRequest restRequest)[0x00028] / Users / stuartlodge / Document中的MvvmCross.Plugins.Network.Rest.MvxRestClient + c__AnonStorey2。<> m__4()[0x0000d] s/GitHub/Mvx/MvvmCross/Plugins/Cirrious/Network/Cirrious.MvvmCross.Plugins.Network/Rest/MvxRestClient.cs:60 at Cirrious.MvvmCross.Plugins.Network.Rest.MvxRestClient.TryCatch (System.Action toTry, System.Action`1 errorAction) [0x00000] in /Users/stuartlodge/Documents/GitHub/Mvx/MvvmCross/Plugins/Cirrious/Network/Cirrious.MvvmCross.Plugins.Network/Rest/MvxRestClient.cs:20 s / GitHub / Mvx / MvvmCross / Plugins / Cirrious / Network / Cirrious.MvvmCross.Plugins.Network / Rest / MvxRestClient.cs:60 at Cirrious.MvvmCross.Plugins.Network.Rest.MvxRestClient.TryCatch(System.Action .Action`1 errorAction)/Users/stuartlodge/Documents/GitHub/Mvx/MvvmCross/Plugins/Cirrious/Network/Cirrious.MvvmCross.Plugins.Network/Rest/MvxRestClient.cs:20中的[0x00000]

That is the code that works on Mac and that i'd like to port using MvvMCross Network Plugin 那是在Mac上工作的代码,我想使用MvvMCross网络插件进行移植

HttpWebRequest request = WebRequest.Create("http://api.myaddress.com/api") as HttpWebRequest;  
        request.Method = "GET";
        request.Accept = "application/json";
        request.Headers.Add(HttpRequestHeader.Cookie, "mycookievalue");

        // Get response  

        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) {  
            // Get the response stream  
            StreamReader reader = new StreamReader (response.GetResponseStream());  
            content = reader.ReadToEnd();

        }

        SearchItem itemssr = jsonConvert.DeserializeObject<SearchItem>(content);

That particular part of the Network plugin is currently under development - see GitHub Issue for REST plugin Network插件的特定部分目前正在开发中-请参阅REST插件的GitHub Issue

It looks as though the cookie feature isn't going to work on the platform you are testing (which I guess from the stacktrace is xamarin.ios) cookie功能似乎无法在您正在测试的平台上运行(我从stacktrace中猜想是xamarin.ios)

In this one case, this might be down to temporary issues in Xamarin.iOS - see: 在这种情况下,这可能归因于Xamarin.iOS中的临时问题-请参阅:


More generally for Plugin development, where a feature doesn't work, then whoever is writing the plugin will have to either override that functionality on that particular platform or will have to mark that feature is NotImplemented on that platform. 更一般而言,对于某个功能无法使用的插件开发,那么无论谁编写该插件,都将不得不覆盖该特定平台上的该功能,或者必须标记该功能在该平台上未实现。

Note that marking features as not implemented is perfectly normal - there are always somethings that will not be implemented on some platforms. 请注意,将功能标记为未实现是完全正常的-某些平台上始终不会实现某些功能。

Assuming the feature can be implemented then whoever is contributing to the network plugin might: 假设可以实现该功能,那么为网络插件做出贡献的人可能会:

If this isn't a temporary Xamarin.iOS issue, then I expect that someone will add cookie support in this way at some point soon. 如果这不是Xamarin.iOS的临时问题,那么我希望有人很快会以这种方式添加cookie支持。 If you want that support sooner, then: 如果您希望尽快获得支持,则:

  • ask on the GitHub issue, giving the contributors a use case (someone might prioritise it) 在GitHub问题上提问,为贡献者提供一个用例(可能会优先考虑)
  • contribute the code yourself 自己贡献代码

If working with this existing Network plugin is too large an initial leap, then maybe start by looking at a simpler plugin - see the much simpler Vibrate example in https://speakerdeck.com/cirrious/plugins-in-mvvmcross 如果使用此现有的Network插件太大了,那么可以从看一个简单的插件开始-参见https://speakerdeck.com/cirrious/plugins-in-mvvmcross中更简单的Vibrate示例


If you don't want to mess around with plugins, then just find some other way of moving forwards - eg using your own native non-PCL library for that part of the functionality. 如果您不想弄乱插件,那么只需找到其他前进的方式-例如使用您自己的本机非PCL库来实现该功能。


Side note: 边注:

That code infact works fine on Xamarin Studio on Mac. 该代码实际上可以在Mac上的Xamarin Studio上正常运行。

As I tried to explain in the other question, currently XS has 'fake' PCL support - so PCLs built in XS are just normal assemblies and are not portable. 正如我试图在另一个问题中解释的那样,当前XS具有“伪造”的PCL支持-因此,内置在XS中的PCL只是普通的程序集, 不能移植。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM