1. 程式人生 > >HttpClient Post 以表單提交方式請求 帶引數

HttpClient Post 以表單提交方式請求 帶引數

/*
	 * **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());
		
		
	}