使用Java在网页中的许多可能选择中选择一个

时间:2014-03-16 19:15:02

标签: java html

我想在以下网址获取以下网页(http://www.studenti.ict.uniba.it/esse3/ListaAppelliOfferta.do)的HTML代码:

  1. 在Facoltà
  2. 中选择“Dipartimento di Informatica”
  3. 选择“Informatica”(或其中一个可用的)
  4. 点击“Avvia Ricerca”
  5. 我对此事并不十分热衷,但我注意到每次选择后页面的网址都保持不变!?

    任何人都可以帮忙描述,我可以详细说明如何做到这一点?不幸的是,我不是网络编程专家。

    非常感谢

1 个答案:

答案 0 :(得分:1)

经过一些测试后,它会使用POST请求刷新页面

fac_id:1012 --
cds_id:197  -- 
ad_id: -- Attività didattica
docente_id:  -- Id of the docent selected
data:06/03/2014 -- Date

无论如何,你错过了Attività ditatticaDocenteData esame的价值

只需使用HttpURLConnection(?)使用此POST参数运行HTTP请求,并使用XML解析器读取tplmessage表的输出。

尝试本教程的HTTP请求:click

尝试阅读本文以了解如何解析响应:click


使用教程代码的示例:

HttpURLConnection connection = null;
try
{
    URL url = new URL("http://www.studenti.ict.uniba.it/esse3/ListaAppelliOfferta.do");
    connection = (HttpURLConnection) url.openConnection(); // open the connection with the url

    String params =
            "fac_id=1012&cds_id=197"; // You need to add ad_id, docente_id and data

    connection.setRequestMethod("POST"); // i need to use POST request method
    connection.setRequestProperty("Content-Length", "" + Integer.toString(params.getBytes().length)); // It will add the length of params

    connection.setRequestProperty("Content-Language", "it-IT"); // language italian

    connection.setUseCaches (false);
    connection.setDoInput   (true);
    connection.setDoOutput  (true);

    DataOutputStream wr = new DataOutputStream(
            connection.getOutputStream ());
    wr.writeBytes (params); // pass params
    wr.flush (); // send request
    wr.close ();

    //Get Response
    InputStream is = connection.getInputStream();
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    String line;
    StringBuilder response = new StringBuilder();
    while((line = rd.readLine()) != null) {
        response.append(line);
        response.append('\r');
    }
    rd.close();
}
catch (MalformedURLException e)
{
    e.printStackTrace();
} catch (IOException e)
{
    e.printStackTrace();
}
finally
{
    // close connection if created
    if (connection != null)
        connection.disconnect();
}

response中,您将拥有该页面的DOM。


无论如何,使用Chrome开发人员工具获取请求参数:

aw