使用POST操作从URL读取信息

时间:2015-05-26 18:21:10

标签: html forms url post action

我正在用Java编写一个程序,要求我从学院的网站上提取信息(具体的菜单项)。鉴于今天的菜单,我可以做到这一点。在网页上,如果我想查看明天的菜单,它只会将#menu附加到当前网址。如果我将该特定URL放入我的程序中,它仍然会为我提供今天的菜单。基本上,我需要弄清楚如何正确地改变URL以获得当天和任何一天的任何一餐的菜单。我对HTML没有太多经验,但这里是网站源代码的一部分。我很感激任何建议!

</table></div> 
<div id="cs_control_38750" class="cs_control CS_Element_CustomCF">
<div id="CS_CCF_9441_38750">
<link rel="stylesheet" type="text/css" media="screen" href="/living/style/lac_menuitems.css" />

<a name="menu"></a>
<FORM action="#menu" method="POST" name="menuform" id="menuform">
<select name="menudates" id="menudates" onchange="submit();">
    <option value="2015-05-21">05/21/2015</option>
    <option value="2015-05-22">05/22/2015</option>
    <option value="2015-05-23">05/23/2015 ... 
</select>
<select name="menuperiod" id="menuperiod" onchange="submit();">
    <option value="Breakfast">Breakfast</option>
    <option value="Brunch">Brunch</option>
    <option value="Lunch" selected>Lunch</option>
    <option value="Dinner">Dinner</option>
</select>
<select name="menulocations" id="menulocations" onchange="submit();">
....
</select>

1 个答案:

答案 0 :(得分:0)

当您更改其中一个选择字段时,表单会提交。然后:

  1. 数据会发布到与表单相同的网址。

  2. 页面滚动到名为&#34;菜单&#34;的链接即原始表格。

  3. 除非服务器在幕后做了很多花哨的东西,否则你可以通过提交&#34;提交&#34;来轻松复制这种行为。您对HTTP请求中您自己的表单值到具有该表单的网址。

    您还可以打开浏览器控制台并观看所有HTTP请求(谷歌如果您不知道如何)。标题将显示在某个显而易见的地方以及您可能需要的任何其他数据。在我的浏览器上,网址会被记录到控制台,点击它会打开与所有标题的对话框。

    我没有多少Java经验,但看起来您需要这样做:Sending HTTP POST Request In Java。标题应如下所示:

    menudates:2015-05-21 menuperiod:晚餐 其他选项:未显示

    我在另一个问题上编辑了代码:

    HttpClient httpclient = HttpClients.createDefault();
    HttpPost httppost = new HttpPost("http://www.url-of-school.com/menu/");
    
    // Request parameters and other properties.
    List<NameValuePair> params = new ArrayList<NameValuePair>(2);
    params.add(new BasicNameValuePair("menudates", "2015-05-21"));
    params.add(new BasicNameValuePair("menuperiod", "Dinner"));
    params.add(new BasicNameValuePair("other-options", "not-shown"));
    httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
    
    //Execute and get the response.
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    
    if (entity != null) {
        InputStream instream = entity.getContent();
        try {
            // do something useful
        } finally {
            instream.close();
        }
    }