How to authenticate permanently using Restsharp

authentication c# html-agility-pack rest restsharp


I'm working on a program that parses data from the web using HTMLAgilityPack. To access the data, you must log in, so it's not quite that simple.

To authenticate, I'm using Restsharp in the following way:

        //class definitions 
        request = new RestRequest("", Method.POST);
        request.AddParameter("utf8", "%E2%9C%93");
        request.AddParameter("authenticity_token", "Lr+JtLv7TkZrS73u5scRPbWhuUYDaVF7vd6lkKFF3FKYqNKHircT9v5WCT9EkneTJRsKXaA6nf6fiWopDB0INw==");
        request.AddParameter("referer", url);  // url to the page I want to go
        request.AddParameter("user[name]", "username");
        request.AddParameter("user[password]", "password");
        request.AddParameter("commit", "Log in");
        request.AddHeader("content-type", "application/x-www-form-urlencoded");
        response = client.Execute(request);

This works wonderfully, but the issue is that I can't log in permanently and I can't use this instance of the RestRequest class to make another request since it will fail with the message "You are not logged in." I've discovered a workaround, but certain sites still won't accept it. Basically, I inserted theurl from thereferer area, but it is annoying for me to have to submit a request each time I go to a different page.

How can I log in indefinitely so that I may submit fresh requests using the same instance?

I'm grateful.

7/27/2015 2:37:57 AM

Popular Answer

I did some study and discovered that cookies are the solution. The answer includes cookies each time a request is made. I did this with the cookies.

After response = client.Execute(request) I included this:

        request.Resource = url;
        request.Method = Method.GET;
        foreach (var cookie in response.Cookies)
            request.AddCookie(cookie.Name , cookie.Value);  //this adds every cookie in the previous response.
        response = client.Execute(request);
       //use the response as required

This utilizes the same instance rather than creating a new one.request to submit other requests, and throughout it all, I am logged in.

I appreciate everyone's assistance.

7/27/2015 4:22:48 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow