无法使用 RestSharp 发送 cookie [英] Unable to send cookies with RestSharp
问题描述
我一直在尝试使用几种不同的方法在 Windows Phone 上访问基于 REST 的 API,但我似乎遇到了将 cookie 附加到所有这些请求的问题.我已经尝试了 WebClient
方法(现在似乎已标记为 SecurityCritical,因此您不能再继承它并添加代码).我简要地看了看 HttpWebRequest
,它充其量看起来很麻烦.
I have been trying to access a REST-based API on a Windows Phone using a few different approaches, but I seem to be running into issues with attaching cookies to the request with all of them. I have tried the WebClient
approach (which now seems to have become marked SecurityCritical, so you can no longer inherit from it and add code). I looked briefly at HttpWebRequest
which seemed cumbersome at best.
现在我使用的 RestSharp 看起来不错,但是我的 cookie 在发送时没有添加到请求中仍然存在问题.
Now I am using RestSharp which seems decent to use, but I am still having problems with my cookies not being added to the request when it's sent.
我的代码如下:
// ... 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.
If anyone has any tips as to why the cookies aren't being sent to the server, please let me know :) I am using RestSharp 101.3 and .Net 4.
推荐答案
RestSharp 102.4 似乎已经解决了这个问题.
RestSharp 102.4 seems to have fixed this problem.
request.AddParameter(_cookie_name, _cookie_value, ParameterType.Cookie);
或者,就你而言
request.AddParameter(this.gallerySessionIdKey, this.sessionId, ParameterType.Cookie);
会正常工作.
这篇关于无法使用 RestSharp 发送 cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!