Java / Scala | Enter the site

I have a problem. I want to load some information from a website into XML (I know how to do this), I know how to get information from a website, but the problem is that I need to log in first.

Its part of the code:

 <form method="post" action="logowanie.php">
  <table class="center">
   <tr>
    <td><label for="username">Login:</label></td>
    <td><input type="text" name="username" value=""></td>
    <td rowspan="2"><div class="submit"><input type="submit" value="OK" name="submit"></div></td>
   </tr>
   <tr>
    <td><label for="password">HasΕ‚o:</label></td>
    <td><input type="password" name="password" value=""></td>

I need someone to enter the user password and click the submit button. Any ideas, code examples, how to do this? Many thanks.

I am doing this in Java / Scala.

+5
source share
2 answers

While Apache HttpClientwill work, using it from Scala is uniomatic and verbose, and the Dispatch library provides a nice Scala wrapper that allows you to write much more concise code. You can use it as follows:

import dispatch._

val u = url("https://site.com/logowanie.php")
val info = Seq("username" -> "me", "password" -> "secret")

val client = new Http
client(u << info >>> System.out)

, , client , .

, , :

import dispatch._
import dispatch.jsoup.JSoupHttp._

val u = url("https://myuni.edu/something/login")
val info = Seq(
  "username"  -> "me",
  "password"  -> "secret",
  "warn"      -> "true",
  "submit"    -> "LOGIN",
  "execution" -> "e1s1",
  "_eventId"  -> "submit"
)

val client = new Http
val id = client(u </> (_.select("input[name=lt]").first.attr("value")))
client(u << info :+ ("lt" -> id) >>> System.out)

Dispatch JSoup , , .

+4

All Articles