0

The "*" are my credentials to log in, and are set to variables. When I run the code, my returned value is a string stating my conditional statement, else value. I am pretty sure I am thinking in the right direction, but somewhere in my thought process its getting mixed up. I hope its a simple fix. Also bare with me I am a firefighter not a coder lol

using HtmlAgilityPack;
using System;
using System.Net;

class Program
{
    static void Main(string[] args)
    {
        string url = "https://app.bryx911.com/login";
        string[] keywords = { "M02", "E56", "L25" };
        string username = "**********";
        string password = "*******";

        using (var handler = new HttpClientHandler { CookieContainer = new CookieContainer(), UseCookies = true })
        {
            using (HttpClient client = new HttpClient(handler))
            {
                var loginContent = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair<string, string>("Email...", username),
                    new KeyValuePair<string, string>("Password...", password)
                });

                var loginResult = client.PostAsync("https://app.bryx911.com/login", loginContent).Result;

                if (loginResult.IsSuccessStatusCode)
                {
                    var responseContent = client.GetAsync(url).Result.Content.ReadAsStringAsync().Result;

                    HtmlDocument doc = new HtmlDocument();
                    doc.LoadHtml(responseContent);

                    foreach (string keyword in keywords)
                    {
                        if (doc.DocumentNode.InnerText.Contains(keyword))
                        {
                            Console.WriteLine($"Found '{keyword}' on the page.");
                        }
                        else
                        {
                            Console.WriteLine($"Could not find '{keyword}' on the page.");
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Login failed.");
                }
            }
        }
    }
}


Thank you in advance, much appreciated.

  • Based on the website, the values you are using for the "Key" portion of your KeyValue pairs are probably wrong. You are using the placeholder text for the inputs rather than the variable names the login script expects. – Erik Kinstler Mar 04 '23 at 17:46
  • Try swapping "Email..." to "username" and "Password..." to "password" – Erik Kinstler Mar 04 '23 at 17:48
  • @ErikKinstler I have tried your suggestion and I still get the message "Login failed". – ワッツアンドレ Mar 04 '23 at 18:05
  • https://stackoverflow.com/questions/14445208/python-authenticate-and-launch-private-page-using-webbrowser-urllib-and-cookiej This seems related, you might want to check if out. – Erik Kinstler Mar 04 '23 at 18:17
  • I have viewed that post before asking this one. I have also tried using cookies as well with no success ? I have even asked chatgpt lol – ワッツアンドレ Mar 04 '23 at 18:21

1 Answers1

0

TLDR: You are submitting the login data to the wrong URL, you are using the wrong key names and the data should not be form-urlencoded.


You are making a POST request to https://app.bryx911.com/login, however this is just the webpage with the login form. You have to make the request to the URL where form actually submits the data.

For forms that are submitted in the traditional way, you can find the URL by inspecting the action attribute of the form element. For forms that are submitted via JavaScript, one way to find the URL is to open the Dev Tools in a browser, switch to the Network tab and click the submit button. A new request record of type POST (or PUT or DELETE, depending on the type of form) will appear.

In your case, the form seems to be submitted via JavaScript. By using the method described above, you will see that the URL where you need to post the login data is https://bryx911.com/api/2.2/session/.

enter image description here

Also, notice that the key names are not Email... and Password.... These are just placeholder text. The endpoint expects a JSON payload in the request body, with a credentials object with the fields email, method and password. There are other things in the JSON payload that may or may not be required:

{
    "credentials": {
        "method": "email",
        "email": "test",
        "password": "test"
    },
    "hardwareId": "*********",
    "hardwareInfo": {
        "manufacturer": null,
        "make": "******",
        "model": null,
        "osName": "*****",
        "osVersion": "****"
    },
    "service": {
        "type": "bryx911",
        "deviceName": "Bryx 911 Universal App - *********",
        "features": {
            "passcode": false
        },
        "engine": "ws",
        "canUseForLocation": true,
        "pushToken": "*********"
    },
    "serviceVersion": "v1.4.5"
}

(I redacted some of the field values, but you can see the overall structure.)

Finally, since the request needs a JSON payload in the body, you don't need to use form url encoding. Just build the JSON and submit it.

Asotos
  • 995
  • 11
  • 14