11

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.

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.

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);
    });
}

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.

Heinrich Ulbricht
  • 10,064
  • 4
  • 54
  • 85
NiteLite
  • 186
  • 1
  • 1
  • 5
  • How are you determining that the cookies aren't on the request? – E.Z. Hart Jul 03 '11 at 07:50
  • I have both been running Fiddler as a proxy for the emulator and, to make sure, I tried pointing it at a PHP file that just printed $_COOKIE. – NiteLite Jul 03 '11 at 12:07
  • 1
    I am going to leave this link here for prosperity, in case anyone else has the same problem. It seems to be an issue with the RestSharp code: http://groups.google.com/group/restsharp/browse_thread/thread/d93f73e9e300ba43 – NiteLite Jul 03 '11 at 13:04

3 Answers3

12

RestSharp 102.4 seems to have fixed this problem.

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

or, in your case

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

will work fine.

Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
2

I had the same problem, and after hours I tried with:

request.AddParameter()
request.AddHeader("Cookie", Cookie value);
and another ways, finnally the solution was using:
request.AddCookie(cookie name, cookie value);
request.AddCookie(cookie name, cookie value);      

I hope that can solve the problem.

Nathan
  • 1,303
  • 12
  • 26
younes
  • 21
  • 1
-1

HttpWebRequest is the best to use. Simply user the CookieContainer to work with cookies. But you have to keep a reference of your CookieContainer over all your requests to get this work

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

cc must be referenced in your instance to be be reused on other request.

Morti
  • 605
  • 5
  • 19