Java Code Examples for cn.hutool.http.HttpRequest#execute()

The following examples show how to use cn.hutool.http.HttpRequest#execute() . 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: OcrUtils.java    From tools-ocr with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static String sogouWebOcr(byte[] imgData) {
    String url = "https://deepi.sogou.com/api/sogouService";
    String referer = "https://deepi.sogou.com/?from=picsearch&tdsourcetag=s_pctim_aiomsg";
    String imageData = Base64.encode(imgData);
    long t = System.currentTimeMillis();
    String sign = SecureUtil.md5("sogou_ocr_just_for_deepibasicOpenOcr" + t + imageData.substring(0, Math.min(1024, imageData.length())) + "4b66a37108dab018ace616c4ae07e644");
    Map<String, Object> data = new HashMap<>();
    data.put("image", imageData);
    data.put("lang", "zh-Chs");
    data.put("pid", "sogou_ocr_just_for_deepi");
    data.put("salt", t);
    data.put("service", "basicOpenOcr");
    data.put("sign", sign);
    HttpRequest request = HttpUtil.createPost(url).timeout(15000);
    request.form(data);
    request.header("Referer", referer);
    HttpResponse response = request.execute();
    return extractSogouResult(WebUtils.getSafeHtml(response));
}
 
Example 2
Source File: CommUtils.java    From tools-ocr with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static String postMultiData(String url, byte[] data, String boundary, String cookie, String referer) {
    try {
        HttpRequest request = HttpUtil.createPost(url).timeout(15000);
        request.contentType("multipart/form-data; boundary=" + boundary);
        request.body(data);
        if (StrUtil.isNotBlank(referer)) {
            request.header("Referer", referer);
        }
        if (StrUtil.isNotBlank(cookie)) {
            request.cookie(cookie);
        }
        HttpResponse response = request.execute();
        return WebUtils.getSafeHtml(response);
    } catch (Exception ex) {
        StaticLog.error(ex);
        return null;
    }
}
 
Example 3
Source File: TomcatManageService.java    From Jpom with MIT License 6 votes vote down vote up
/**
 * 查询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;
}
 
Example 4
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 5
Source File: AbstractOneDriveServiceBase.java    From zfile with MIT License 6 votes vote down vote up
/**
 * 根据 RefreshToken 刷新 AccessToken, 返回刷新后的 Token.
 *
 * @return  刷新后的 Token
 */
public OneDriveToken getRefreshToken() {
    StorageConfig refreshStorageConfig =
            storageConfigRepository.findByDriveIdAndKey(driveId, StorageConfigConstant.REFRESH_TOKEN_KEY);

    String param = "client_id=" + getClientId() +
            "&redirect_uri=" + getRedirectUri() +
            "&client_secret=" + getClientSecret() +
            "&refresh_token=" + refreshStorageConfig.getValue() +
            "&grant_type=refresh_token";

    String fullAuthenticateUrl = AUTHENTICATE_URL.replace("{authenticateEndPoint}", getAuthenticateEndPoint());
    HttpRequest post = HttpUtil.createPost(fullAuthenticateUrl);

    post.body(param, "application/x-www-form-urlencoded");
    HttpResponse response = post.execute();
    return JSONObject.parseObject(response.body(), OneDriveToken.class);
}
 
Example 6
Source File: AbstractOneDriveServiceBase.java    From zfile with MIT License 6 votes vote down vote up
/**
 * OAuth2 协议中, 根据 code 换取 access_token 和 refresh_token.
 *
 * @param   code
 *          代码
 *
 * @return  获取的 Token 信息.
 */
public OneDriveToken getToken(String code) {
    String param = "client_id=" + getClientId() +
            "&redirect_uri=" + getRedirectUri() +
            "&client_secret=" + getClientSecret() +
            "&code=" + code +
            "&scope=" + getScope() +
            "&grant_type=authorization_code";

    String fullAuthenticateUrl = AUTHENTICATE_URL.replace("{authenticateEndPoint}", getAuthenticateEndPoint());
    HttpRequest post = HttpUtil.createPost(fullAuthenticateUrl);

    post.body(param, "application/x-www-form-urlencoded");
    HttpResponse response = post.execute();
    return JSONObject.parseObject(response.body(), OneDriveToken.class);
}
 
Example 7
Source File: WebHookUtil.java    From Jpom with MIT License 5 votes vote down vote up
/**
 * 发送钉钉群自定义机器人消息
 *
 * @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_UTF8_VALUE).
            body(param.toJSONString());
    request.execute();
}
 
Example 8
Source File: WebUtils.java    From tools-ocr with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static HttpResponse get(String url, int userAgent, Map<String, String> headers, boolean allowRedirct) {
    try {
        HttpRequest request = HttpUtil.createGet(url).timeout(10000).setFollowRedirects(allowRedirct);
        if (headers == null) {
            headers = new Hashtable<>();
        }
        switch (userAgent) {
            case 1:
                headers.put("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 9_3_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13F69 MicroMessenger/6.3.16 NetType/WIFI Language/zh_CN");
                break;
            case 2:
                headers.put("User-Agent", "Mozilla/5.0 (Linux; U; Android 2.2; en-gb; GT-P1000 Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1");
                break;
            case 3:
                headers.put("User-Agent", "Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; NOKIA; Lumia 930) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Mobile Safari/537.36 Edge/13.10586");
                break;
            case 4:
                headers.put("User-Agent", "NativeHost");
                break;
            case 5:
                headers.put("User-Agent", "Dalvik/1.6.0 (Linux; U; Android 4.4.2; NoxW Build/KOT49H) ITV_5.7.1.46583");
                break;
            case 6:
                headers.put("User-Agent", "qqlive");
                break;
            case 7:
                headers.put("User-Agent", "Dalvik/1.6.0 (Linux; U; Android 4.2.2; 6S Build/JDQ39E)");
                break;
            case 8:
                headers.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) XIAMI-MUSIC/3.0.9 Chrome/56.0.2924.87 Electron/1.6.11 Safari/537.36");
                break;
            case 9:
                headers.put("User-Agent", "okhttp/2.7.5");
                break;
            case 10:
                headers.put("User-Agent", "Mozilla/5.0 (Linux; Android 5.1.1; oppo r11 plus Build/LMY48Z) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/39.0.0.0 Mobile Safari/537.36 SogouSearch Android1.0 version3.0");
                break;
            default:
                headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36");
                break;
        }
        request.addHeaders(headers);
        return request.execute();
    } catch (Exception ex) {
        StaticLog.error(ex);
        return null;
    }
}
 
Example 9
Source File: WebUtils.java    From tools-ocr with GNU Lesser General Public License v3.0 4 votes vote down vote up
private static HttpResponse postData(String url, Map<String, Object> data, int contentType, int userAgent, Map<String, String> headers) {
    try {
        HttpRequest request = HttpUtil.createPost(url).timeout(10000);
        if (contentType == 0) {
            request.contentType("application/x-www-form-urlencoded");
            request.form(data);
        } else if (contentType == 1) {
            request.body(data.values().iterator().next().toString(), "application/json;charset=UTF-8");
        } else {
            request.contentType("application/x-www-form-urlencoded");
            request.body(data.values().iterator().next().toString());
        }
        if (headers == null) {
            headers = new Hashtable<>();
        }
        switch (userAgent) {
            case 1:
                headers.put("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3");
                break;
            case 2:
                headers.put("User-Agent", "Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19");
                break;
            case 3:
                headers.put("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; NOKIA; Lumia 920)");
                break;
            case 4:
                headers.put("User-Agent", "NativeHost");
                break;
            case 5:
                headers.put("User-Agent", "Apache-HttpClient/UNAVAILABLE (java 1.4)");
                break;
            case 6:
                headers.put("User-Agent", "Mozilla/5.0 (iPad; CPU OS 8_1_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12B466 Safari/600.1.4");
                break;
            case 7:
                headers.put("User-Agent", "okhttp/2.7.5");
                break;
            case 10:
                headers.put("User-Agent", "Mozilla/5.0 (Linux; Android 5.1.1; oppo r11 plus Build/LMY48Z) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/39.0.0.0 Mobile Safari/537.36 SogouSearch Android1.0 version3.0");
                break;
            default:
                headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36");
                break;
        }
        request.addHeaders(headers);
        return request.execute();
    } catch (Exception ex) {
        StaticLog.error(ex);
        return null;
    }
}