c# – 无法使用RestSharp发送cookie

我一直在尝试使用几种不同的方法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.

解决方法

RestSharp 102.4似乎解决了这个问题.
request.AddParameter(_cookie_name,_cookie_value,ParameterType.Cookie);

或者,在你的情况下

request.AddParameter(this.gallerySessionIdKey,ParameterType.Cookie);

会很好的.

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...