use of cn.hutool.http.HttpRequest in project Jpom by dromara.
the class WebHookUtil method send.
/**
* 发送钉钉群自定义机器人消息
*
* @param notify 通知对象
* @param title 描述标签
* @param context 消息内容
*/
@Override
public void send(MonitorModel.Notify notify, String title, String context) {
JSONObject text = new JSONObject();
JSONObject param = new JSONObject();
// 消息内容
text.put("content", title + "\n" + context);
param.put("msgtype", "text");
param.put("text", text);
HttpRequest request = HttpUtil.createPost(notify.getValue()).contentType(MediaType.APPLICATION_JSON_VALUE).body(param.toJSONString());
request.execute();
}
use of cn.hutool.http.HttpRequest in project Jpom by dromara.
the class TestGithub method testUser.
@Test
public void testUser() {
HttpRequest request = HttpUtil.createGet("https://api.github.com/user");
request.header("Authorization", "token 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");
request.header("Accept", "application/vnd.github.v3+json");
String body = request.execute().body();
System.out.println(body);
}
use of cn.hutool.http.HttpRequest in project Jpom by dromara.
the class TestGithub method test.
@Test
public void test() {
HttpRequest post = HttpUtil.createPost("https://api.github.com/repositories?since=824");
String body = post.execute().body();
System.out.println(body);
}
use of cn.hutool.http.HttpRequest in project Jpom by dromara.
the class RepositoryController method githubRepos.
/**
* github 仓库
*
* @param token 个人令牌
* @param page 分页
* @return page
*/
private PageResultDto<JSONObject> githubRepos(String token, Page page) {
String accept = "application/vnd.github.v3+json";
HttpRequest request = HttpUtil.createGet("https://api.github.com/user");
request.header("Authorization", StrUtil.format("token {}", token));
request.header("Accept", accept);
HttpResponse httpResponse = request.execute();
String body = httpResponse.body();
Assert.state(httpResponse.isOk(), "令牌信息错误:" + body);
JSONObject userBody = JSONObject.parseObject(body);
String username = userBody.getString("login");
// 拉取仓库信息
HttpRequest httpRequestRepos = HttpUtil.createGet("https://api.github.com/user/repos");
httpRequestRepos.header("Authorization", StrUtil.format("token {}", token));
httpRequestRepos.header("Accept", accept);
HttpResponse reposResponse = httpRequestRepos.form("access_token", token).form("sort", "pushed").form("page", page.getPageNumber()).form("per_page", page.getPageSize()).execute();
body = reposResponse.body();
Assert.state(reposResponse.isOk(), "拉取仓库信息错误:" + body);
JSONArray jsonArray = JSONArray.parseArray(body);
List<JSONObject> objects = jsonArray.stream().map(o -> {
JSONObject repo = (JSONObject) o;
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", repo.getString("name"));
String cloneUrl = repo.getString("clone_url");
jsonObject.put("url", cloneUrl);
jsonObject.put("full_name", repo.getString("full_name"));
jsonObject.put("description", repo.getString("description"));
jsonObject.put("private", repo.getBooleanValue("private"));
//
jsonObject.put("username", username);
jsonObject.put("exists", RepositoryController.this.checkRepositoryUrl(null, cloneUrl));
return jsonObject;
}).collect(Collectors.toList());
//
PageResultDto<JSONObject> pageResultDto = new PageResultDto<>(page.getPageNumber(), page.getPageSize(), 1000);
pageResultDto.setResult(objects);
return pageResultDto;
}
use of cn.hutool.http.HttpRequest in project Jpom by dromara.
the class AbstractTomcatCommander method getStatus.
/**
* 检查tomcat状态
*
* @param tomcatInfoModel tomcat信息
* @param cmd 操作命令
* @return 状态结果
*/
protected String getStatus(TomcatInfoModel tomcatInfoModel, String cmd) {
String strReturn = "start".equals(cmd) ? "stopped" : "started";
int i = 0;
while (i < 10) {
int result = 0;
String url = String.format("http://127.0.0.1:%d/", tomcatInfoModel.getPort());
HttpRequest httpRequest = new HttpRequest(url);
// 设置超时时间为3秒
httpRequest.setConnectionTimeout(3000);
try {
httpRequest.execute();
result = 1;
} catch (Exception ignored) {
}
i++;
if ("start".equals(cmd) && result == 1) {
strReturn = "started";
break;
}
if ("stop".equals(cmd) && result == 0) {
strReturn = "stopped";
break;
}
ThreadUtil.sleep(1000);
}
return strReturn;
}
Aggregations