记录使用emqx http api 踢除指定客户端

CloseableHttpClient httpClient = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000).setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();
// emqx账号:密码, 例如:admin:public        
String authString = "admin".trim() + ":" + "public".trim();
String authStringEnc = Base64.getEncoder().encodeToString(authString.getBytes());
// url: http://localhost:18083/api/v4/clients/ + clientsid
String url = "http://localhost:18083/api/v4/clients/" + "20012001";
LogUtil.info("url=" + url);
HttpDelete httpDelete = new HttpDelete(url);
httpDelete.setHeader("Authorization", "Basic " + authStringEnc);
httpDelete.setHeader("Content-type", "application/json");
httpDelete.setHeader("DataEncoding", "UTF-8");
httpDelete.setConfig(requestConfig);
CloseableHttpResponse httpResponse = null;
try {
    httpResponse = httpClient.execute(httpDelete);
    HttpEntity entity = httpResponse.getEntity();
    String result = EntityUtils.toString(entity);
    LogUtil.info("result=" + result);
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (httpResponse != null) {
        try {
            httpResponse.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    if (null != httpClient) {
        try {
            httpClient.close();

        } catch (IOException e) {
            e.printStackTrace();

        }
    }
}
Logo

加入社区!打开量化的大门,首批课程上线啦!

更多推荐