sonicbit

mail@pastecode.io avatar
unknown
csharp
8 months ago
3.0 kB
6
Indexable
Never
 static public async Task LoginAndUploadMagnet(string email, string password)
    {
        var url = "https://dash.sonicbit.net/login";
        var handler = new HttpClientHandler() {};
        var httpClient = new HttpClient(handler);

        var htmlContent = await httpClient.GetStringAsync(url);

        var htmlDocument = new HtmlDocument();
        htmlDocument.LoadHtml(htmlContent);
        Console.WriteLine(htmlDocument.Text);
        var csrfTokenNode = htmlDocument.DocumentNode.SelectSingleNode("//meta[@name='csrf-token']");
        if (csrfTokenNode == null)
        {
            Console.WriteLine("CSRF token not found");
            return;
        }
        var csrfToken = csrfTokenNode.GetAttributeValue("content", string.Empty);
        Console.WriteLine(csrfToken);
        string userAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.12 Safari/535.11";
        var headers = new Dictionary<string, string>
{
    {"User-Agent", userAgent}
};

        var loginData = new Dictionary<string, string>
{
    {"_token", csrfToken},
    {"_method", "POST"},
    {"email", email},
    {"password", password},
};

        var loginEndpoint = "https://dash.sonicbit.net/login_user";
        var initialResponse = await httpClient.GetAsync(url);
        if (!initialResponse.Headers.Contains("Set-Cookie"))
        {
            Console.WriteLine("No cookies found in the response");
            return;
        }
        var cookieContainer = new CookieContainer();
        handler.CookieContainer = cookieContainer;
        using (var client = new HttpClient(handler) { BaseAddress = new Uri(url) })
        {
            foreach (var cookie in initialResponse.Headers.GetValues("Set-Cookie"))
            {
                cookieContainer.SetCookies(new Uri(url), cookie);
            }

            client.DefaultRequestHeaders.Clear();
            foreach (var header in headers)
            {
                client.DefaultRequestHeaders.Add(header.Key, header.Value);
            }

            var response = await client.PostAsync(loginEndpoint, new FormUrlEncodedContent(loginData));

            while (response.StatusCode == HttpStatusCode.Redirect ||
                   response.StatusCode == HttpStatusCode.MovedPermanently)
            {
                var redirectUrl = response.Headers.Location;
                Console.WriteLine($"Redirecting to: {redirectUrl}");
                response = await client.GetAsync(redirectUrl);
            }

            var responseContent = await response.Content.ReadAsStringAsync();
            Console.WriteLine(responseContent);

            var cookies = cookieContainer.GetCookies(new Uri(url));
            foreach (Cookie cookie in cookies)
            {
                Console.WriteLine($"{cookie.Name}: {cookie.Value}");
            }
        }
    }
Leave a Comment