HttpClient Get請求帶引數
阿新 • • 發佈:2018-11-09
/* * HttpClient Get請求帶引數 */ @Test public void fun1() throws ClientProtocolException, IOException { // 1、建立httpClient CloseableHttpClient client = HttpClients.createDefault(); // 2、封裝請求引數 List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>(); list.add(new BasicNameValuePair("username", "cgx")); list.add(new BasicNameValuePair("password", "123456")); //3、轉化引數 String params = EntityUtils.toString(new UrlEncodedFormEntity(list,Consts.UTF_8)); System.out.println(params); //4、建立HttpGet請求 HttpGet httpGet = new HttpGet("http://localhost:8080/itcast297/loginAction_login"+"?"+params); CloseableHttpResponse response = client.execute(httpGet); //5、獲取實體 HttpEntity entity = response.getEntity(); //將實體裝成字串 String string = EntityUtils.toString(entity); System.out.println(string); response.close(); }