Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to send cookies with RestSharp

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.

like image 305
NiteLite Avatar asked Jul 02 '11 12:07

NiteLite


People also ask

What is RestSharp DLL?

RestSharp is a comprehensive, open-source HTTP client library that works with all kinds of DotNet technologies. It can be used to build robust applications by making it easy to interface with public APIs and quickly access data without the complexity of dealing with raw HTTP requests.

What is RestRequest C#?

RestSharp is a C# library used to build and send API requests, and interpret the responses. It is used as part of the C#Bot API testing framework to build the requests, send them to the server, and interpret the responses so assertions can be made.


2 Answers

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.

like image 76
Sathyajith Bhat Avatar answered Nov 07 '22 15:11

Sathyajith Bhat


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.

like image 2
younes Avatar answered Nov 07 '22 16:11

younes