`

      // 发送请求HTTP-POST请求 url:请求地址; entity:json格式请求参数
      public static String post(String url, String entity) {
	try {
              String username="user";// 账户
              String password="1234";// 密码		      
              CloseableHttpClient httpClient = HttpClients.createDefault();
              HttpPost httpPost = new HttpPost(url);
              httpPost.addHeader("Authorization", "Basic " + java.util.Base64.getUrlEncoder().encodeToString((username + ":" + password).getBytes()));
              StringEntity se = new StringEntity(entity, "UTF-8");
              se.setContentType("application/json");
              httpPost.setEntity(se);
              CloseableHttpResponse response = httpClient.execute(httpPost);
              HttpEntity entity1 = response.getEntity();
              String resStr = null;
              if (entity1 != null) {
                  resStr = EntityUtils.toString(entity1, "UTF-8");
              }
              httpClient.close();
              response.close();
              return resStr;
	  } catch (Exception e) {
              e.printStackTrace();
	  }
	  return "";
      }

`

posted on 2020-12-21 15:55  黎明前的守护  阅读(12420)  评论(0)    收藏  举报