Failed to send cookies using RestSharp

I'm trying to access the REST-based API on Windows Phone using several different approaches, but I seem to be having problems attaching cookies to the request with all of them. I tried the approach WebClient(which now seems to have become SecurityCritical marked, so you can no longer inherit it and add code). I glanced briefly at HttpWebRequestwhich seemed bulky at best.

Now I use RestSharp, which seems decent to use, but I'm still having problems with my cookies not being added to the request when it is sent.

My code is as follows:

// ... 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.

+5
2

RestSharp 102.4, , .

 request.AddParameter(_cookie_name, _cookie_value, ParameterType.Cookie);

,

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

.

+4

HttpWebRequest . CookieContainer cookie. CookieContainer , .

CookieContainer cc = new CookieContainer();
HttpWebRequest webRequest = HttpWebRequest.CreateHttp(uri);
webRequest.CookieContainer = cc;
webRequest.BeginGetResponse((callback)=>{//Code on callback},webRequest);

cc .

-1

All Articles