HttpClient Access to HTML Form

1 篇文章 0 订阅
1 篇文章 0 订阅

Sample Html Form Code:

<s:form action="Add" method="post">
		<s:textfield label="ISBN" name="book.isbn"></s:textfield>
		<s:textfield label="Name" name="book.name"></s:textfield>
		<s:textfield label="Price" name="book.price"></s:textfield>
		<s:submit></s:submit>
</s:form>

 

Java Access html form through HttpClient Code:

public static void form() {
		HttpClient httpClient = new DefaultHttpClient();
		try {
			List<NameValuePair> formparams = new ArrayList<NameValuePair>();
			formparams.add(new BasicNameValuePair("book.isbn", "753-95487621"));
			formparams.add(new BasicNameValuePair("book.name", "HttpClient"));
			formparams.add(new BasicNameValuePair("book.price", "45.6"));
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams,
					"UTF-8");
			HttpPost httpPost = new HttpPost(
					"http://localhost:8080/struts2/book/Add.action");
			httpPost.setEntity(entity);
			HttpResponse response = httpClient.execute(httpPost);
			HttpEntity httpEntity = response.getEntity();
			System.out.println("Add form get: " + response.getStatusLine());
			EntityUtils.consume(entity);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			httpClient.getConnectionManager().shutdown();
			System.out.println("ok");
		}
	}

 

main method:

form();

 

 Reference:http://svn.apache.org/viewvc/httpcomponents/oac.hc3x/trunk/src/examples/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值