FlareSolverrSharp
FlareSolverrSharp copied to clipboard
Added cookies support
Adds support for FlareSolverr v3.2.0's cookies.
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}");
}
}
}
}
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.