String url = "http://127.0.0.1:8099/login"; String json = "{\"login_name\": \"18800000000\",\"login_password\": \"123456\"}"; @Test public void post() throws ClientProtocolException, IOException{ //创建client CloseableHttpClient httpclient = HttpClients.createDefault(); try { //创建post请求 HttpPost httppost = new HttpPost(url); //json StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON); httppost.setEntity(entity); System.out.println("executing request " + httppost.getRequestLine()); //执行post请求 CloseableHttpResponse response = httpclient.execute(httppost); try { System.out.println("----------------------------------------"); //状态 System.out.println(response.getStatusLine()); //响应实体 HttpEntity resEntity = response.getEntity(); if (resEntity != null) { System.out.println(EntityUtils.toString(resEntity)); } //关闭HttpEntity流 EntityUtils.consume(resEntity); } finally { response.close(); } } finally { httpclient.close(); } }