FlareSolverrSharp icon indicating copy to clipboard operation
FlareSolverrSharp copied to clipboard

Added cookies support

Open FFace32 opened this issue 2 years ago • 2 comments

Adds support for FlareSolverr v3.2.0's cookies.

FFace32 avatar Oct 10 '23 12:10 FFace32

Hello! @ilike2burnthing you've asked me to provide some test code here for this and #27. I did come back with a reply, albeit late, and I'm assuming you didn't get notified about it since the PR was merged. I'm reposting here:

Make sure you're using .NET8 when compiling!

using FlareSolverrSharp.Solvers;
using FlareSolverrSharp.Types;
using System.Reflection;

namespace Playground
{
    class Program
    {
        static async Task Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine($"Usage: dotnet run {Assembly.GetExecutingAssembly().Location} <FlareSolverr URL>");
                return;
            }

            FlareSolverr solver = new(args[0]);

            FlareSolverrIndexResponse indexResponse = await solver.GetIndex();
            Console.WriteLine("GetIndex() returned the following:");
            Console.WriteLine($"  Message = '{indexResponse.Message}'");
            Console.WriteLine($"  Version = '{indexResponse.Version}'");
            Console.WriteLine($"  UserAgent = '{indexResponse.UserAgent}'");
            Console.WriteLine("Now, I'll save the UserAgent and use it WITHOUT having to call Solve() and do an expensive request.\n");

            // Code that uses indexResponse.UserAgent goes here

            Uri uri = new("https://setcookie.net/");
            HttpRequestMessage request = new(HttpMethod.Get, uri);

            Console.WriteLine($"Calling Solve() on {uri} with no cookies...");
            FlareSolverrResponse response = await solver.Solve(request);

            if (response.Solution.Response.Contains("Received no cookies."))
            {
                Console.WriteLine("Received no cookies, as expected.");
            }
            else
            {
                Console.WriteLine("Received cookies, which is unexpected!");
            }

            Console.WriteLine($"Calling Solve() on {uri} with 2 cookies...");
            response = await solver.Solve(request, cookies: [
                new Cookie() { Name = "test", Value = "value" },
                new Cookie() { Name = "test2", Value = "value2" }
            ]);

            if (response.Solution.Response.Contains("Received no cookies."))
            {
                Console.WriteLine("Received no cookies, which is unexpected!");
            }
            else if (response.Solution.Response.Contains("test = value") &&
                    response.Solution.Response.Contains("test2 = value2"))
            {
                Console.WriteLine("Received cookies, as expected.");
            }
            else
            {
                Console.WriteLine($"Something went wrong, here's the response:\n{response.Solution.Response}");
            }
        }
    }
}

FFace32 avatar Jun 01 '24 21:06 FFace32

Apologies, I did see it, and I thought I'd replied with at least a 'thanks', but seemingly not. My focus at the time (I need to go back to it actually) was with https://github.com/FlareSolverr/FlareSolverr/pull/1163.

I'll deal with this ASAP.

ilike2burnthing avatar Jun 01 '24 22:06 ilike2burnthing