Java Code Examples for cn.hutool.http.HttpResponse#body()

The following examples show how to use cn.hutool.http.HttpResponse#body() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Test1.java    From SubTitleSearcher with Apache License 2.0 6 votes vote down vote up
public static String getUrl(String url) {
	try {
		HttpResponse response = HttpRequest.get(url).execute();

		System.out.println(response.toString());
		int statusCode = response.getStatus();
		if (statusCode == 301 || statusCode == 302) {
			String location = response.header("Location");
			if (!location.toLowerCase().startsWith("http")) {
				location = StringUtil.getBaseUrl(url) + location;
			}
			return getUrl(location);
		} else if (statusCode == 200) {
			return response.body();
		} else {
			System.out.println(url + ", failed: " + statusCode);
			return null;
		}
	} catch (Exception e) {
		e.printStackTrace();
		return null;
	} finally {
	}
}
 
Example 2
Source File: TomcatManageService.java    From Jpom with MIT License 6 votes vote down vote up
/**
 * 访问tomcat Url
 *
 * @param tomcatInfoModel tomcat信息
 * @param cmd             命令
 * @return 访问结果
 */
private String tomcatCmd(TomcatInfoModel tomcatInfoModel, String cmd) {
    String url = String.format("http://127.0.0.1:%d/jpomAgent/%s", tomcatInfoModel.getPort(), cmd);
    HttpRequest httpRequest = new HttpRequest(url);
    // 设置超时时间为3秒
    httpRequest.setConnectionTimeout(3000);
    String body = "";

    try {
        HttpResponse httpResponse = httpRequest.execute();
        if (httpResponse.isOk()) {
            body = httpResponse.body();
        }
        if (httpResponse.getStatus() == HttpStatus.HTTP_NOT_FOUND) {
            // 没有插件
            tomcatInfoModel.initTomcat();
            throw new JpomRuntimeException("tomcat 未初始化,已经重新初始化请稍后再试");
        }
    } catch (JpomRuntimeException jpom) {
        throw jpom;
    } catch (Exception ignored) {
    }

    return body;
}
 
Example 3
Source File: Test1.java    From SubTitleSearcher with Apache License 2.0 6 votes vote down vote up
public static String getUrl(String url) {
	try {
		HttpResponse response = HttpRequest.get(url).execute();

		System.out.println(response.toString());
		int statusCode = response.getStatus();
		if (statusCode == 301 || statusCode == 302) {
			String location = response.header("Location");
			if (!location.toLowerCase().startsWith("http")) {
				location = StringUtil.getBaseUrl(url) + location;
			}
			return getUrl(location);
		} else if (statusCode == 200) {
			return response.body();
		} else {
			System.out.println(url + ", failed: " + statusCode);
			return null;
		}
	} catch (Exception e) {
		e.printStackTrace();
		return null;
	} finally {
	}
}
 
Example 4
Source File: ManualOperateController.java    From spring-boot-demo with MIT License 6 votes vote down vote up
/**
 * 测试手动保存任务
 */
@GetMapping("/add")
public String xxlJobAdd() {
    Map<String, Object> jobInfo = Maps.newHashMap();
    jobInfo.put("jobGroup", 2);
    jobInfo.put("jobCron", "0 0/1 * * * ? *");
    jobInfo.put("jobDesc", "手动添加的任务");
    jobInfo.put("author", "admin");
    jobInfo.put("executorRouteStrategy", "ROUND");
    jobInfo.put("executorHandler", "demoTask");
    jobInfo.put("executorParam", "手动添加的任务的参数");
    jobInfo.put("executorBlockStrategy", ExecutorBlockStrategyEnum.SERIAL_EXECUTION);
    jobInfo.put("glueType", GlueTypeEnum.BEAN);

    HttpResponse execute = HttpUtil.createGet(baseUri + JOB_INFO_URI + "/add").form(jobInfo).execute();
    log.info("【execute】= {}", execute);
    return execute.body();
}
 
Example 5
Source File: ManualOperateController.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 测试手动启动任务
 */
@GetMapping("/start")
public String xxlJobStart() {
    Map<String, Object> jobInfo = Maps.newHashMap();
    jobInfo.put("id", 4);

    HttpResponse execute = HttpUtil.createGet(baseUri + JOB_INFO_URI + "/start").form(jobInfo).execute();
    log.info("【execute】= {}", execute);
    return execute.body();
}
 
Example 6
Source File: ManualOperateController.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 测试手动停止任务
 */
@GetMapping("/stop")
public String xxlJobStop() {
    Map<String, Object> jobInfo = Maps.newHashMap();
    jobInfo.put("id", 4);

    HttpResponse execute = HttpUtil.createGet(baseUri + JOB_INFO_URI + "/stop").form(jobInfo).execute();
    log.info("【execute】= {}", execute);
    return execute.body();
}
 
Example 7
Source File: ManualOperateController.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 测试手动删除任务
 */
@GetMapping("/remove")
public String xxlJobRemove() {
    Map<String, Object> jobInfo = Maps.newHashMap();
    jobInfo.put("id", 4);

    HttpResponse execute = HttpUtil.createGet(baseUri + JOB_INFO_URI + "/remove").form(jobInfo).execute();
    log.info("【execute】= {}", execute);
    return execute.body();
}
 
Example 8
Source File: ManualOperateController.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 测试手动触发一次任务
 */
@GetMapping("/trigger")
public String xxlJobTrigger() {
    Map<String, Object> jobInfo = Maps.newHashMap();
    jobInfo.put("id", 4);
    jobInfo.put("executorParam", JSONUtil.toJsonStr(jobInfo));

    HttpResponse execute = HttpUtil.createGet(baseUri + JOB_INFO_URI + "/trigger").form(jobInfo).execute();
    log.info("【execute】= {}", execute);
    return execute.body();
}
 
Example 9
Source File: ManualOperateController.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 分页任务列表
 *
 * @param page 当前页,第一页 -> 0
 * @param size 每页条数,默认10
 * @return 分页任务列表
 */
@GetMapping("/list")
public String xxlJobList(Integer page, Integer size) {
    Map<String, Object> jobInfo = Maps.newHashMap();
    jobInfo.put("start", page != null ? page : 0);
    jobInfo.put("length", size != null ? size : 10);
    jobInfo.put("jobGroup", 2);
    jobInfo.put("triggerStatus", -1);

    HttpResponse execute = HttpUtil.createGet(baseUri + JOB_INFO_URI + "/pageList").form(jobInfo).execute();
    log.info("【execute】= {}", execute);
    return execute.body();
}
 
Example 10
Source File: ManualOperateController.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 任务组列表,xxl-job叫做触发器列表
 */
@GetMapping("/group")
public String xxlJobGroup() {
    HttpResponse execute = HttpUtil.createGet(baseUri + JOB_GROUP_URI + "/list").execute();
    log.info("【execute】= {}", execute);
    return execute.body();
}
 
Example 11
Source File: HtHttpUtil.java    From SubTitleSearcher with Apache License 2.0 5 votes vote down vote up
public String postStream(String url, String data, String charset, String ua, String refer) {
	logger.info("postStream=" + url);
	try {
		HttpResponse response = addHeader(HttpRequest.post(url).body(data, _stream_media_type)
				.header(Header.USER_AGENT, ua).header(Header.ACCEPT_CHARSET, charset)
				.charset(charset).header(Header.ACCEPT, _accept)
				.header(Header.ACCEPT_ENCODING, _accept_encoding)
				.header(Header.REFERER, refer != null ? refer : url)
				.header(Header.ACCEPT_LANGUAGE, _accept_language).timeout(_time_out)).execute();

		if (debug) {
			logger.info("response header:" + getRespHeaderStr(response));
		}
		int statusCode = response.getStatus();
		if (statusCode == 301 || statusCode == 302) {
			String location = response.header("Location");
			if (!location.toLowerCase().startsWith("http")) {
				location = StringUtil.getBaseUrl(url) + location;
			}
			return postStream(location, data, charset, ua, refer);
		} else if (statusCode == 200) {
			return response.body();
		} else {
			logger.error(url + ", failed: " + statusCode);
			return null;
		}
	} catch (Exception e) {
		logger.error(e);
		return null;
	} finally {
	}

}
 
Example 12
Source File: HtHttpUtil.java    From SubTitleSearcher with Apache License 2.0 5 votes vote down vote up
public String post(String url, Map<String, Object> list, String charset, String ua, String refer) {
	logger.info("post=" + url);
	try {
		HttpResponse response = addHeader(HttpRequest.post(url).form(list).header(Header.USER_AGENT, ua)
				.header(Header.ACCEPT_CHARSET, charset).charset(charset)
				.header(Header.ACCEPT, _accept).header(Header.ACCEPT_ENCODING, _accept_encoding)
				.header(Header.REFERER, refer != null ? refer : url)
				.header(Header.ACCEPT_LANGUAGE, _accept_language).timeout(_time_out)).execute();

		if (debug) {
			logger.info("response header:" + getRespHeaderStr(response));
		}
		int statusCode = response.getStatus();
		if (statusCode == 301 || statusCode == 302) {
			String location = response.header("Location");
			if (!location.toLowerCase().startsWith("http")) {
				location = StringUtil.getBaseUrl(url) + location;
			}
			return post(location, list, charset, ua, refer);
		} else if (statusCode == 200) {
			return response.body();
		} else {
			logger.error(url + ", failed: " + statusCode);
			return null;
		}
	} catch (Exception e) {
		logger.error(e);
		return null;
	} finally {
	}
}
 
Example 13
Source File: HtHttpUtil.java    From SubTitleSearcher with Apache License 2.0 5 votes vote down vote up
public String get(String url, String charset, String ua, String refer) {
	logger.info("get=" + url);

	try {
		HttpResponse response = addHeader(HttpRequest.get(url).header(Header.USER_AGENT, ua)
				.header(Header.ACCEPT_CHARSET, charset).charset(charset)
				.header(Header.ACCEPT, _accept).header(Header.ACCEPT_ENCODING, _accept_encoding)
				.header(Header.REFERER, refer != null ? refer : url)
				.header(Header.ACCEPT_LANGUAGE, _accept_language).timeout(_time_out)).execute();

		if (debug) {
			logger.info(response.toString());
		}
		int statusCode = response.getStatus();
		if (statusCode == 301 || statusCode == 302) {
			String location = response.header("Location");
			if (!location.toLowerCase().startsWith("http")) {
				location = StringUtil.getBaseUrl(url) + location;
			}
			return get(location, charset, ua, refer);
		} else if (statusCode == 200) {
			return response.body();
		} else {
			logger.error(url + ", failed: " + statusCode);
			return null;
		}
	} catch (Exception e) {
		logger.error(e);
		return null;
	} finally {
	}
}
 
Example 14
Source File: HtHttpUtil.java    From SubTitleSearcher with Apache License 2.0 5 votes vote down vote up
public String postStream(String url, String data, String charset, String ua, String refer) {
	logger.info("postStream=" + url);
	try {
		HttpResponse response = addHeader(HttpRequest.post(url).body(data, _stream_media_type)
				.header(Header.USER_AGENT, ua).header(Header.ACCEPT_CHARSET, charset)
				.charset(charset).header(Header.ACCEPT, _accept)
				.header(Header.ACCEPT_ENCODING, _accept_encoding)
				.header(Header.REFERER, refer != null ? refer : url)
				.header(Header.ACCEPT_LANGUAGE, _accept_language).timeout(_time_out)).execute();

		if (debug) {
			logger.info("response header:" + getRespHeaderStr(response));
		}
		int statusCode = response.getStatus();
		if (statusCode == 301 || statusCode == 302) {
			String location = response.header("Location");
			if (!location.toLowerCase().startsWith("http")) {
				location = StringUtil.getBaseUrl(url) + location;
			}
			return postStream(location, data, charset, ua, refer);
		} else if (statusCode == 200) {
			return response.body();
		} else {
			logger.error(url + ", failed: " + statusCode);
			return null;
		}
	} catch (Exception e) {
		logger.error(e);
		return null;
	} finally {
	}

}
 
Example 15
Source File: HtHttpUtil.java    From SubTitleSearcher with Apache License 2.0 5 votes vote down vote up
public String post(String url, Map<String, Object> list, String charset, String ua, String refer) {
	logger.info("post=" + url);
	try {
		HttpResponse response = addHeader(HttpRequest.post(url).form(list).header(Header.USER_AGENT, ua)
				.header(Header.ACCEPT_CHARSET, charset).charset(charset)
				.header(Header.ACCEPT, _accept).header(Header.ACCEPT_ENCODING, _accept_encoding)
				.header(Header.REFERER, refer != null ? refer : url)
				.header(Header.ACCEPT_LANGUAGE, _accept_language).timeout(_time_out)).execute();

		if (debug) {
			logger.info("response header:" + getRespHeaderStr(response));
		}
		int statusCode = response.getStatus();
		if (statusCode == 301 || statusCode == 302) {
			String location = response.header("Location");
			if (!location.toLowerCase().startsWith("http")) {
				location = StringUtil.getBaseUrl(url) + location;
			}
			return post(location, list, charset, ua, refer);
		} else if (statusCode == 200) {
			return response.body();
		} else {
			logger.error(url + ", failed: " + statusCode);
			return null;
		}
	} catch (Exception e) {
		logger.error(e);
		return null;
	} finally {
	}
}
 
Example 16
Source File: HtHttpUtil.java    From SubTitleSearcher with Apache License 2.0 5 votes vote down vote up
public String get(String url, String charset, String ua, String refer) {
	logger.info("get=" + url);

	try {
		HttpResponse response = addHeader(HttpRequest.get(url).header(Header.USER_AGENT, ua)
				.header(Header.ACCEPT_CHARSET, charset).charset(charset)
				.header(Header.ACCEPT, _accept).header(Header.ACCEPT_ENCODING, _accept_encoding)
				.header(Header.REFERER, refer != null ? refer : url)
				.header(Header.ACCEPT_LANGUAGE, _accept_language).timeout(_time_out)).execute();

		if (debug) {
			logger.info(response.toString());
		}
		int statusCode = response.getStatus();
		if (statusCode == 301 || statusCode == 302) {
			String location = response.header("Location");
			if (!location.toLowerCase().startsWith("http")) {
				location = StringUtil.getBaseUrl(url) + location;
			}
			return get(location, charset, ua, refer);
		} else if (statusCode == 200) {
			return response.body();
		} else {
			logger.error(url + ", failed: " + statusCode);
			return null;
		}
	} catch (Exception e) {
		logger.error(e);
		return null;
	} finally {
	}
}
 
Example 17
Source File: WebUtils.java    From tools-ocr with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static String getSafeHtml(HttpResponse response) {
    if (response == null) {
        return "";
    }
    return response.body();
}