Search in sources :

Example 51 with HttpRequest

use of com.github.kevinsawicki.http.HttpRequest in project dubidubi by lzzzz4.

the class LocationServiceImpl method getCity.

/**
 * @Description: 调取高德得到城市方法
 * @data: @param Longitude
 * @data: @param Latitude
 * @data: @return
 * @date: 2018年1月10日下午3:21:57
 */
@Override
public GaoDeAddressDTO getCity(Double Longitude, Double Latitude) {
    HttpRequest request = HttpRequest.get("http://restapi.amap.com/v3/geocode/regeo?key=" + key + "&location=" + Longitude + "," + Latitude);
    String body = request.body();
    // System.out.println(body);
    JSONObject jsonObject = JSON.parseObject(body);
    JSONObject location = jsonObject.getJSONObject("regeocode");
    GaoDeAddressDTO gaoDeAddress = new GaoDeAddressDTO();
    gaoDeAddress.setFormatted_address(location.getString("formatted_address"));
    JSONObject address = location.getJSONObject("addressComponent");
    gaoDeAddress.setAdcode(address.getString("adcode"));
    // System.out.println(gaoDeAddress);
    return gaoDeAddress;
}
Also used : HttpRequest(com.github.kevinsawicki.http.HttpRequest) JSONObject(com.alibaba.fastjson.JSONObject) GaoDeAddressDTO(cn.dubidubi.model.dto.GaoDeAddressDTO)

Example 52 with HttpRequest

use of com.github.kevinsawicki.http.HttpRequest in project dubidubi by lzzzz4.

the class WxBaseServiceImpl method getRealAccessToken.

private String getRealAccessToken() {
    HttpRequest request = HttpRequest.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + WxProperties.APP_Id + "&secret=" + WxProperties.APP_SECRET);
    request.trustAllCerts();
    request.trustAllHosts();
    String body = request.body();
    JSONObject jsonObject = JSON.parseObject(body);
    String access_token = jsonObject.getString("access_token");
    LoggerFactory.getLogger(this.getClass()).info("获取accesstoken");
    return access_token;
}
Also used : HttpRequest(com.github.kevinsawicki.http.HttpRequest) JSONObject(com.alibaba.fastjson.JSONObject)

Example 53 with HttpRequest

use of com.github.kevinsawicki.http.HttpRequest in project dubidubi by lzzzz4.

the class WxBaseServiceImpl method codeToAccessToken.

@Override
public String codeToAccessToken(String code) {
    String wxUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + WxProperties.APP_Id + "&secret=" + WxProperties.APP_SECRET + "&code=" + code + "&grant_type=authorization_code";
    HttpRequest httpRequest = HttpRequest.get(wxUrl);
    httpRequest.trustAllCerts();
    httpRequest.trustAllHosts();
    String json = httpRequest.body();
    JSONObject jsonObject = new JSONObject();
    jsonObject = JSON.parseObject(json);
    // 拉取信息
    String getInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + jsonObject.getString("access_token") + "&openid=" + jsonObject.getString("openid") + "&lang=zh_CN";
    HttpRequest info = HttpRequest.get(getInfoUrl);
    String userInfo = info.body();
    return userInfo;
}
Also used : HttpRequest(com.github.kevinsawicki.http.HttpRequest) JSONObject(com.alibaba.fastjson.JSONObject)

Example 54 with HttpRequest

use of com.github.kevinsawicki.http.HttpRequest in project DiscordSRV by Scarsz.

the class DebugUtil method uploadToBin.

private static String uploadToBin(String binHost, int aesBits, List<Map<String, String>> files, String description) {
    String key = RandomStringUtils.randomAlphanumeric(aesBits == 256 ? 32 : 16);
    byte[] keyBytes = key.getBytes();
    // decode to bytes, encrypt, base64
    List<Map<String, String>> encryptedFiles = new ArrayList<>();
    for (Map<String, String> file : files) {
        Map<String, String> encryptedFile = new HashMap<>(file);
        encryptedFile.entrySet().removeIf(entry -> StringUtils.isBlank(entry.getValue()));
        encryptedFile.replaceAll((k, v) -> b64(encrypt(keyBytes, file.get(k))));
        encryptedFiles.add(encryptedFile);
    }
    Map<String, Object> payload = new HashMap<>();
    payload.put("description", b64(encrypt(keyBytes, description)));
    payload.put("expiration", TimeUnit.DAYS.toMinutes(7));
    payload.put("files", encryptedFiles);
    HttpRequest request = HttpRequest.post(binHost + "/v1/post").userAgent("DiscordSRV " + DiscordSRV.version).send(GSON.toJson(payload));
    if (request.code() == 200) {
        Map json = GSON.fromJson(request.body(), Map.class);
        if (json.get("status").equals("ok")) {
            return binHost + "/" + json.get("bin") + "#" + key;
        } else {
            String reason = "";
            if (json.containsKey("error")) {
                Map error = (Map) json.get("error");
                reason = ": " + error.get("type") + " " + error.get("message");
            }
            throw new RuntimeException("Bin upload status wasn't ok" + reason);
        }
    } else {
        throw new RuntimeException("Got bad HTTP status from Bin: " + request.code());
    }
}
Also used : HttpRequest(com.github.kevinsawicki.http.HttpRequest)

Example 55 with HttpRequest

use of com.github.kevinsawicki.http.HttpRequest in project PowerNukkitX by BlocklyNukkit.

the class DownloadDialog method init.

private void init() {
    {
        this.setTitle(LanguageUtils.tr("gui.download-dialog.title", name));
        this.setSize(600, 108);
        this.setLayout(null);
        this.setVisible(true);
        this.setResizable(false);
    }
    final JPanel panel = new JPanel(null);
    this.setContentPane(panel);
    label = new JLabel(link);
    {
        panel.add(label);
        label.setSize(400, 36);
        label.setLocation(8, 0);
    }
    final JProgressBar progressBar = new JProgressBar(0, 100);
    {
        progressBar.setStringPainted(true);
        progressBar.setOrientation(HORIZONTAL);
        panel.add(progressBar);
        progressBar.setLocation(8, 36);
        progressBar.setSize(new Dimension(578, 16));
    }
    new SwingWorker<Void, Void>() {

        @Override
        protected Void doInBackground() {
            final File dir = target.getParentFile();
            if (dir != null && !dir.exists()) {
                // noinspection ResultOfMethodCallIgnored
                dir.mkdirs();
            }
            final HttpRequest request = HttpRequest.get(link);
            final AtomicLong atomicLong = new AtomicLong(0);
            CommonModel.TIMER.scheduleAtFixedRate(new TimerTask() {

                @Override
                public void run() {
                    final long finished = target.length();
                    final long total = request.getConnection().getContentLength();
                    final long speed = finished - atomicLong.get();
                    atomicLong.set(finished);
                    SwingUtilities.invokeLater(() -> {
                        progressBar.setValue((int) Math.min(100, (finished * 1.0 / total) * 100));
                        label.setText(StringUtils.displayableBytes(speed) + "/s - " + link);
                    });
                    if (finished == total) {
                        this.cancel();
                    }
                }
            }, 500, 500);
            request.receive(target);
            return null;
        }

        @Override
        protected void done() {
            self.dispose();
            callback.accept(self);
        }
    }.execute();
}
Also used : HttpRequest(com.github.kevinsawicki.http.HttpRequest) AtomicLong(java.util.concurrent.atomic.AtomicLong) TimerTask(java.util.TimerTask) File(java.io.File)

Aggregations

HttpRequest (com.github.kevinsawicki.http.HttpRequest)86 HttpRequestException (com.github.kevinsawicki.http.HttpRequest.HttpRequestException)29 IOException (java.io.IOException)25 JSONObject (org.json.JSONObject)19 UnsupportedEncodingException (java.io.UnsupportedEncodingException)13 File (java.io.File)8 TimerTask (java.util.TimerTask)8 AtomicLong (java.util.concurrent.atomic.AtomicLong)8 Pair (android.util.Pair)6 URL (java.net.URL)5 JSONException (org.json.JSONException)5 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)4 JSONObject (com.alibaba.fastjson.JSONObject)4 ZipFile (net.lingala.zip4j.ZipFile)4 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)3 IPEntity (com.example.li.springboot_crawler_demo.utils.img.entity.IPEntity)2 HttpURLConnection (java.net.HttpURLConnection)2 MalformedURLException (java.net.MalformedURLException)2 Matcher (java.util.regex.Matcher)2 JSONArray (org.json.JSONArray)2