我正在尝试发出需要嵌入 cookie 的休息请求。我以这种方式使用 System.Net 引用做到了这一点
参考 PCL 和 MVVMCross 上的 System.net
但我在 VS2012 上移植该 PCL 库(System.Net)时遇到问题。该代码实际上在 Mac 上的 Xamarin Studio 上运行良好。
提到那个问题,我尝试以这种方式使用 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; });
我有这个错误“请求的功能没有实现。”
在 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) 系统.Net.HttpWebRequest:get_SupportsCookieContainer () 在 Cirrious.MvvmCross.Plugins.Network.Rest.MvxRestClient.SetCookieContainer (Cirrious.MvvmCross.Plugins.Network.Rest.MvxRestRequest restRequest, System.Net.HttpWebRequest httpRequest) [0x00000] 在 /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] 在 /Users/stuartlodge/Documents/GitHub/Mvx/MvvmCross/Plugins/Cirrious/Network/Cirrious.MvvmCross.Plugins。Network/Rest/MvxRestClient.cs:100 在 Cirrious.MvvmCross.Plugins.Network.Rest.MvxRestClient+c__AnonStorey2.<>m__4 () [0x0000d] 在 /Users/stuartlodge/Documents/GitHub/Mvx/MvvmCross/Plugins/Cirrious/ Network/Cirrious.MvvmCross.Plugins.Network/Rest/MvxRestClient.cs:60 在 Cirrious.MvvmCross.Plugins.Network.Rest.MvxRestClient.TryCatch (System.Action toTry, System.Action`1 errorAction) [0x00000] 在 /Users /stuartlodge/Documents/GitHub/Mvx/MvvmCross/Plugins/Cirrious/Network/Cirrious.MvvmCross.Plugins.Network/Rest/MvxRestClient.cs:20Network.Rest.MvxRestClient.TryCatch (System.Action toTry, System.Action`1 errorAction) [0x00000] 在 /Users/stuartlodge/Documents/GitHub/Mvx/MvvmCross/Plugins/Cirrious/Network/Cirrious.MvvmCross.Plugins.Network /Rest/MvxRestClient.cs:20Network.Rest.MvxRestClient.TryCatch (System.Action toTry, System.Action`1 errorAction) [0x00000] 在 /Users/stuartlodge/Documents/GitHub/Mvx/MvvmCross/Plugins/Cirrious/Network/Cirrious.MvvmCross.Plugins.Network /Rest/MvxRestClient.cs:20
那是在 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);