我一直在尝试使用几种不同的方法在 Windows Phone 上访问基于 REST 的 API,但我似乎遇到了将 cookie 附加到所有请求的问题。我已经尝试过这种WebClient
方法(现在似乎已标记为 SecurityCritical,因此您不能再从它继承并添加代码)。我简单地看了看HttpWebRequest
,充其量似乎很麻烦。
现在我正在使用看起来不错的 RestSharp,但是在发送请求时我的 cookie 没有被添加到请求中仍然存在问题。
我的代码如下:
// ... some additional support vars ...
private RestClient client;
public ClassName() {
client = new RestClient();
client.BaseUrl = this.baseAddress.Scheme + "://" + baseAddress.DnsSafeHost;
}
public void GetAlbumList()
{
Debug.WriteLine("Init GetAlbumList()");
if (this.previousAuthToken == null || this.previousAuthToken.Length == 0)
{
throw new MissingAuthTokenException();
}
RestRequest request = new RestRequest(this.baseUrl, Method.GET);
// Debug prints the correct key and value, but it doesnt seem to be included
// when I run the request
Debug.WriteLine("Adding cookie [" + this.gallerySessionIdKey + "] = [" + this.sessionId + "]");
request.AddParameter(this.gallerySessionIdKey, this.sessionId, ParameterType.Cookie);
request.AddParameter("g2_controller", "remote:GalleryRemote", ParameterType.GetOrPost);
request.AddParameter("g2_form[cmd]", "fetch-albums-prune", ParameterType.GetOrPost);
request.AddParameter("g2_form[protocol_version]", "2.2", ParameterType.GetOrPost);
request.AddParameter("g2_authToken", this.previousAuthToken, ParameterType.GetOrPost);
// Tried adding a no-cache header in case there was some funky caching going on
request.AddHeader("cache-control", "no-cache");
client.ExecuteAsync(request, (response) =>
{
parseResponse(response);
});
}
如果有人对为什么没有将 cookie 发送到服务器有任何提示,请告诉我 :) 我使用的是 RestSharp 101.3 和 .Net 4。