0

I am working on an app that will help me log in the website and view data that I need. While I have no trouble with making sure that I parse that data and work with it properly, I did face an issue with logging into the website. I tried sending POST request, yet that didn't really work for some reason so I started looking more closely into how POST request to that website is sent in the browser and here is what I got: Picture

I also asked a guy who developed that website and he said that I should use two cookies with "ulogin" and "upassword" for my log in. I tried using JSOUP as shown right here: https://jsoup.org/cookbook/input/load-document-from-url

I used .cookies("upassword", "10101010"), yet it didn't work so it makes me think that there is a bit more to it than just writing a simple line a post request.

Please, can someone explain to me how do I use cookies to log into website or at least point me in the direction where I can learn that, because I am so close to making that app happen and I will be able to proceed further with it's development, but it's just this one step that I am really being stuck with.

Here is an additional picture with Response and Request Headers from the Firefox. Picture

Vitaliy-T
  • 733
  • 6
  • 23
  • Try adding the actual code and avoid adding screenshots. Do learn more about this at https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors. Also, share exact errors and the approaches you have followed before posting the question.Lear more about this here : https://stackoverflow.com/help/how-to-ask – Lazycoder-007 May 26 '20 at 17:21
  • 1
    There are many examples how cookie based authentication works, just google it: https://stackoverflow.com/questions/33934633/servlet-authentication-using-sessions-and-cookies and http://www.mtitek.com/tutorials/samples/java-cookie-auth.php – Pakira May 26 '20 at 17:53

1 Answers1

0

I managed to get it working a long time, yet didn't post an answer. So, here we go.

Cookies are just simple Headers, therefore you should treat them as such. In my case, with the use of HttpURLConnection, here is a piece of working code:

Note: My original request is for Java, however, I have since moved to Kotlin, so this solution uses Kotlin and this function is a "suspend" function which means that it is designed to be used with Kotlin Couroutines.

suspend fun httpRequest(): String {
        val conn: HttpURLConnection = url_profile.openConnection() as HttpURLConnection
        conn.requestMethod = "POST"
        conn.doOutput = true
        conn.doInput = true
        conn.setRequestProperty(
            "Cookie",
            "YOUR COOKIE DATA"
        )
        val input: BufferedReader = BufferedReader(InputStreamReader(conn.inputStream))
        return input.readText()
    }
Vitaliy-T
  • 733
  • 6
  • 23