Search in sources :

Example 6 with HttpRequest

use of cn.hutool.http.HttpRequest in project xiushang-boot by shijingsh.

the class OceanEngineResource method request.

public static String request(HttpMethod httpMethod, String apiUrl, String payLoad, String token) throws OceanEngineRestException {
    try {
        JSONObject jsonObject = JSONObject.parseObject(payLoad);
        Method method = Method.GET;
        if (httpMethod == HttpMethod.POST) {
            method = Method.POST;
        }
        HttpRequest request = HttpUtil.createRequest(method, apiUrl).contentType("application/json").header("Api-Version", "0.1").header("User-Agent", "OceanEngine Marketing-api Java SDK");
        if (StringUtils.isNotBlank(token)) {
            request.header("Access-Token", token);
        }
        String responseString = // 表单内容
        request.form(jsonObject).timeout(// 超时,毫秒
        20000).execute().body();
        ;
        return responseString;
    } catch (Exception e) {
        throw new OceanEngineRestException("api request io error, url=" + apiUrl, e);
    }
}
Also used : HttpRequest(cn.hutool.http.HttpRequest) JSONObject(com.alibaba.fastjson.JSONObject) Method(cn.hutool.http.Method)

Example 7 with HttpRequest

use of cn.hutool.http.HttpRequest in project Jpom by dromara.

the class AgentExtConfigBean method createServerRequest.

/**
 * 创建请求对象
 *
 * @param openApi url
 * @return HttpRequest
 * @see ServerOpenApi
 */
public HttpRequest createServerRequest(String openApi) {
    if (StrUtil.isEmpty(getServerUrl())) {
        throw new JpomRuntimeException("请先配置server端url");
    }
    if (StrUtil.isEmpty(getServerToken())) {
        throw new JpomRuntimeException("请先配置server端Token");
    }
    // 加密
    String md5 = SecureUtil.md5(getServerToken());
    md5 = SecureUtil.sha1(md5 + ServerOpenApi.HEAD);
    HttpRequest httpRequest = HttpUtil.createPost(String.format("%s%s", serverUrl, openApi));
    httpRequest.header(ServerOpenApi.HEAD, md5);
    return httpRequest;
}
Also used : HttpRequest(cn.hutool.http.HttpRequest)

Example 8 with HttpRequest

use of cn.hutool.http.HttpRequest in project Jpom by dromara.

the class TomcatManageService method tomcatCmd.

/**
 * 访问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;
}
Also used : HttpRequest(cn.hutool.http.HttpRequest) JpomRuntimeException(io.jpom.system.JpomRuntimeException) HttpResponse(cn.hutool.http.HttpResponse) JpomRuntimeException(io.jpom.system.JpomRuntimeException)

Example 9 with HttpRequest

use of cn.hutool.http.HttpRequest in project Jpom by dromara.

the class TomcatManageService method getTomcatStatus.

/**
 * 查询tomcat状态
 *
 * @param id tomcat的id
 * @return tomcat状态0表示未运行,1表示运行中
 */
public int getTomcatStatus(String id) {
    int result = 0;
    TomcatInfoModel tomcatInfoModel = tomcatEditService.getItem(id);
    String url = String.format("http://127.0.0.1:%d/", tomcatInfoModel.getPort());
    HttpRequest httpRequest = new HttpRequest(url);
    // 设置超时时间为3秒
    httpRequest.setConnectionTimeout(3000);
    try {
        HttpResponse httpResponse = httpRequest.execute();
        result = 1;
    } catch (Exception ignored) {
    }
    return result;
}
Also used : HttpRequest(cn.hutool.http.HttpRequest) TomcatInfoModel(io.jpom.model.data.TomcatInfoModel) HttpResponse(cn.hutool.http.HttpResponse) JpomRuntimeException(io.jpom.system.JpomRuntimeException)

Example 10 with HttpRequest

use of cn.hutool.http.HttpRequest in project Jpom by dromara.

the class RemoteVersion method loadTransitUrl.

/**
 * 获取第一层信息,用于中转
 *
 * @return 中转URL
 */
private static String loadTransitUrl() {
    String body = StrUtil.EMPTY;
    try {
        HttpRequest request = HttpUtil.createGet(URL);
        body = request.execute().body();
        // 
        JSONObject jsonObject = JSONObject.parseObject(body);
        return jsonObject.getString("url");
    } catch (Exception e) {
        DefaultSystemLog.getLog().warn("获取远程版本信息失败:{} {}", e.getMessage(), body);
        return null;
    }
}
Also used : HttpRequest(cn.hutool.http.HttpRequest) JSONObject(com.alibaba.fastjson.JSONObject) IOException(java.io.IOException)

Aggregations

HttpRequest (cn.hutool.http.HttpRequest)21 Test (org.junit.Test)9 JSONObject (com.alibaba.fastjson.JSONObject)6 Ignore (org.junit.Ignore)5 HttpResponse (cn.hutool.http.HttpResponse)4 JpomRuntimeException (io.jpom.system.JpomRuntimeException)4 File (java.io.File)3 IOException (java.io.IOException)3 JsonMessage (cn.jiangzeyin.common.JsonMessage)2 JSONArray (com.alibaba.fastjson.JSONArray)2 Convert (cn.hutool.core.convert.Convert)1 FileUtil (cn.hutool.core.io.FileUtil)1 Tuple (cn.hutool.core.lang.Tuple)1 Validator (cn.hutool.core.lang.Validator)1 StrUtil (cn.hutool.core.util.StrUtil)1 URLUtil (cn.hutool.core.util.URLUtil)1 Entity (cn.hutool.db.Entity)1 Page (cn.hutool.db.Page)1 ServletUtil (cn.hutool.extra.servlet.ServletUtil)1 HttpUtil (cn.hutool.http.HttpUtil)1