HttpClient Post 以表单提交方式请求 带参数

  • Post author:
  • Post category:其他


/*
	 * **HttpClient Post 以表单提交方式请求 带参数**
	 */
	@Test
	public void fun5() throws ClientProtocolException, IOException{
		//1、创建HttpClient
		org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
		//2、创建get或post请求方法
		PostMethod method = new PostMethod("http://localhost:8080/itcast297/loginAction_login");
		//3、设置编码
		httpClient.getParams().setContentCharset("UTF-8");
		//4、设置请求消息头,为表单方式提交
		method.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
		
		//5、设置参数
		method.setParameter("username", "cgx");
		method.setParameter("password", "123456");
		
//		6、执行提交
		httpClient.executeMethod(method);
		System.out.println(method.getStatusLine());
		System.out.println(method.getResponseBodyAsString());
		
		
	}