0

I'm trying to create a program to log into reddit and do some actions for me, reddit requires:

CSRF, OTP, PASS, DEST, USER

when sending the post request and I am definitely filling all of these with the right data but for some reason I cannot get a response from reddit, the program crashes

`

using System;
using System.Net.Http;
using System.Collections.Generic;
// Set up the HTTP client
static string getBetween(string strSource, string strStart, string strEnd)
{
    if (strSource.Contains(strStart) && strSource.Contains(strEnd))
    {
        int Start, End;
        Start = strSource.IndexOf(strStart, 0) + strStart.Length;
        End = strSource.IndexOf(strEnd, Start);
        return strSource.Substring(Start, End - Start);
    }

    return "";
}

var client = new HttpClient();
client.DefaultRequestHeaders.Add("UserAgent", "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0");
client.DefaultRequestHeaders.Add("Pragma", "no-cache");
client.DefaultRequestHeaders.Add("Accept", "*/*");
client.DefaultRequestHeaders.Add("Referer", "https://www.google.com");
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded");
//client.DefaultRequestHeaders.Add("Content-Type", "application/x-www-form-urlencoded");
client.BaseAddress = new Uri("https://www.reddit.com/");
string csrf = "FAIL";
using (var response = client.GetAsync("https://www.reddit.com/login").Result)
{
    if (response.IsSuccessStatusCode)
    {
        csrf = getBetween(response.Content.ReadAsStringAsync().Result, "csrf_token\" value=\"", "\"");
    }
}
Console.WriteLine(csrf);
// Set up the login form data
var formData = new Dictionary<string, string>
            {
                { "csrf_token", csrf},
                { "otp", "" },
                { "password", "XXXX" },
                { "dest", "https://reddit.com/" },
                { "username", "XXXX" }
            };



// Post the login form
var response4 = await client.PostAsync("https://www.reddit.com/login/", new FormUrlEncodedContent(formData));

// Check the response status code
//if (response.IsSuccessStatusCode)
//{
//    Console.WriteLine("Login successful!");
//}
//else
//{
//    Console.WriteLine("Login failed!");
//}

`

and it seems reddit just doesn't give a response, my program immediately crashes. anyone could give me some insight maybe? thank you

I tried changing it up and playing a bit with the headers to no avail

  • 2
    *"the program crashes"* - It sounds like the code is throwing an exception. Time to use a debugger. What is the exception? Which specific operation produces the exception? What are the observed runtime values used in that operation when this happens? – David Dec 13 '22 at 13:45
  • @David var response4 = await client.PostAsync("https://www.reddit.com/login/", new FormUrlEncodedContent(formData)); is what crashes, I don't know how to use the debugger unfortunately – Amit Morad Dec 13 '22 at 13:54
  • 1
    This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Dec 13 '22 at 13:55
  • okay so I've debugged and I'm getting a bad request, I assumed it was something with the headers but it seems even adding the headers I receive from the GET request crashes the program. perhaps I need to add just a part of the headers? this is very weird – Amit Morad Dec 13 '22 at 14:04
  • 1
    If it's a bad request then it's a bad request. It's really up to the server how to respond to requests. Are you using an API or are you trying to reverse-engineer browser requests? If the former then the API documentation should be useful. If the latter then you're essentially looking at a trial-and-error process. You can use tools like Postman to help you test individual requests, observe responses, etc. and use the results of that testing to build the requests in your application. – David Dec 13 '22 at 14:10
  • sounds good I've pretty much found out what the problem was. thank you – Amit Morad Dec 13 '22 at 14:18
  • If you've solved the problem, please consider posting the solution as an answer so that others can benefit from it. It's fine, even encouraged, to answer your own question - see [Can I answer my own question](https://stackoverflow.com/help/someone-answers) – sbridewell Dec 14 '22 at 09:30

0 Answers0