ELLY66
1七/110

如何使用Apache HtmlComponents提交表单

某日,我哥请求我帮他写一个可以从“大连商品交易所”下载数据的程序。他说,如果按照传统的方法,必须输入一个日期,在点一下按钮。如果要下载上白天的数据,这种方法实在太枯燥冗长了。(果然,什么样的妹妹就有什么样的哥哥。)

我用Java+Apache HtmlComponents实现的。整个过程其实并不复杂。但是,我在学做的时候发现,HtmlComponents的帮助文档实在是不利于初学者理解。网上大多实例是用Html Client3.0,而不是4.0。(Html Client 3.0也是Apache的,不过被抛弃了。Apache重新写了HtmlComponents,由HtmlCore和HtmlClient组成,我是用HtmlClient做的)所以,我还是花了不少时间在Google的。

首先,干这种事,第一步是分析网页的源代码。这是非常重要的一件事情。通过“大连商品交易所”下载数据的过程很简单。打开网页-->填写日期-->单击“下载文本格式”。所以我们主要也是看这一块内容的代码。如果用chrome浏览器的话,鼠标移到下图所示区域,右键点击view frame source. 

先要找到form标签。

<form target="_blank" name="form1" action="/PublicWeb/MainServlet" method="post" onSubmit="return formcheck(this);">

然后找到<form>...</form>中所有的input, select, textarea标签。

本例中有:

<input type="hidden" name="action" value="Pu00011_result">

<input name="Pu00011_Input.trade_date" type="text" size="8" maxlength="8"> 

<select name="Pu00011_Input.variety">

      <option selected value="all">全部</option>

      <option value="a">豆一</option> 

     ......

</select>

<input name="Pu00011_Input.trade_type" type="radio" value="0" checked>

<input name="Submit" type="submit" class="button" value="查 询" onClick="document.form1.action.value='Pu00011_result';document.form1.target='_blank';">

<input name="Submit2" type="submit" class="downbutton" value="下载文本格式" onClick="document.form1.action.value='Pu00012_download';document.form1.target='_self';">

<input name="Submit2" type="submit" class="button" value="打 印" onClick="document.form1.action.value='Pu00011_result';document.form1.target='_blank';">

 

整个表单提交过程是:我把日期赋给name为Pu00011_Input.trade_date的input的value。然后当我按按钮的时候,出发onclick事件,修改了form1.action.value的值。(这里的action指的是form1里嵌套的name为action的input标签,并不是action属性。)然后服务器端通过type为submit的input标签的name和value,以及form1.action.value来执行相应的动作。

下面是java代码。就写了主要的部分。

public class Crawler {
 
	public static void main(String[] args) throws Exception {
		HttpClient httpClient = new DefaultHttpClient();
		HttpPost httpost = new HttpPost(
				"http://www.dce.com.cn/PublicWeb/MainServlet");
		List<namevaluepair> nvps = new ArrayList<namevaluepair>();
		nvps.add(new BasicNameValuePair("action","Pu00012_download"));
		nvps.add(new BasicNameValuePair("Pu00011_Input.trade_date","20110629"));
		nvps.add(new BasicNameValuePair("Pu00011_Input.variety","all"));
		nvps.add(new BasicNameValuePair("Pu00011_Input.trade_type","0"));
		nvps.add(new BasicNameValuePair("Submit2","下载文本格式"));
		httpost.setEntity(new UrlEncodedFormEntity(nvps, "GBK"));
		HttpResponse response = httpClient.execute(httpost);
		HttpEntity entity = response.getEntity();
		System.out.println(response.getStatusLine());   
		System.out.println(entity.getContentLength());
		System.out.println(entity.getContentType());
		File storeFile = new File("C:/test.txt");
		FileOutputStream output = new FileOutputStream(storeFile);
		InputStream input = entity.getContent();
		byte b[] = new byte[1024];
		int j = 0;
		while( (j = input.read(b))!=-1){
		output.write(b,0,j);
		}
		output.close();
	}
</namevaluepair></namevaluepair>

解释一下。

  • HttpPost httpost = new HttpPost("http://www.dce.com.cn/PublicWeb/MainServlet"); 括号中的地址是form标签中,action属性的地址。
  • nvps.add(new BasicNameValuePair("action","Pu00012_download")); 这之后都是加参数。注意,所有的input,select,textarea都要一一写。BasicNameValuePair第一个参数是name,第二哥参数是value. 对于select标签,value写你所选择的option的标签。关于最后的三个type为submit的标签,只需要写你想点击的按钮的name和value。在这里我们选择的是第二个按钮。“下载文本文件”。

这样一来,如果要下载很多天的数据的话,很简单就可以加一个for循环了。

原创文章,转载请注明: 转载自ELLY66

本文链接地址: 如何使用Apache HtmlComponents提交表单

文章的脚注信息由WordPress的wp-posturl插件自动生成

Evernote lets you save all the interesting things you see online into a single place. Access all those saved pages from your computer, phone or the web. Sign up now or learn more. It's free!

 
评论 (0) 引用 (0)

还没有评论.


Leave a comment

(required)

还没有引用.

Switch to our mobile site