Java Code Examples for org.nutz.http.Response#getContent()

The following examples show how to use org.nutz.http.Response#getContent() . 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: LoachClient.java    From nutzcloud with Apache License 2.0 6 votes vote down vote up
protected boolean _ping() {
    try {
        String pingURL = url + "/ping/" + getServiceName() + "/" + id;
        if (isDebug())
            log.debug("Ping URL=" + pingURL);
        Request req = Request.create(pingURL, METHOD.GET);
        req.getHeader().clear();
        req.getHeader().set("If-None-Match", lastPingETag);
        Response resp = Sender.create(req, conf.getInt("loach.client.ping.timeout", 1000)).setConnTimeout(1000).send();
        String cnt = resp.getContent();
        if (isDebug())
            log.debug("Ping result : " + cnt);
        if (resp.isOK()) {
            lastPingETag = Strings.sBlank(resp.getHeader().get("ETag"), "ABC");
            NutMap re = Json.fromJson(NutMap.class, cnt);
            if (re != null && re.getBoolean("ok", false))
                return true;
        } else if (resp.getStatus() == 304) {
            return true;
        }
    }
    catch (Throwable e) {
        log.debugf("bad url? %s %s", url, e.getMessage());
    }
    return false;
}
 
Example 2
Source File: HttpTool.java    From mpsdk4j with Apache License 2.0 6 votes vote down vote up
public static String get(String url) {
    if (log.isDebugEnabled()) {
        log.debugf("Request url: %s, default timeout: %d", url, CONNECT_TIME_OUT);
    }

    try {
        Response resp = Http.get(url, CONNECT_TIME_OUT);
        if (resp.isOK()) {
            String content = resp.getContent("UTF-8");
            if (log.isInfoEnabled()) {
                log.infof("GET Request success. Response content: %s", content);
            }
            return content;
        }

        throw Lang.wrapThrow(new RuntimeException(String.format("Get request [%s] failed. status: %d",
                                                                url,
                                                                resp.getStatus())));
    }
    catch (Exception e) {
        throw Lang.wrapThrow(e);
    }
}
 
Example 3
Source File: HttpTool.java    From mpsdk4j with Apache License 2.0 6 votes vote down vote up
public static String upload(String url, File file) {
    if (log.isDebugEnabled()) {
        log.debugf("Upload url: %s, file name: %s, default timeout: %d",
                   url,
                   file.getName(),
                   CONNECT_TIME_OUT);
    }

    try {
        Request req = Request.create(url, METHOD.POST);
        req.getParams().put("media", file);
        Response resp = new FilePostSender(req).send();
        if (resp.isOK()) {
            String content = resp.getContent();
            return content;
        }

        throw Lang.wrapThrow(new RuntimeException(String.format("Upload file [%s] failed. status: %d",
                                                                url,
                                                                resp.getStatus())));
    }
    catch (Exception e) {
        throw Lang.wrapThrow(e);
    }
}
 
Example 4
Source File: AbstractWxApi2.java    From nutzwx with Apache License 2.0 6 votes vote down vote up
protected void reflushJsapiTicket() {
    String at = this.getAccessToken();
    String url = String.format("%s/ticket/getticket?access_token=%s&type=jsapi", base, at);
    if (log.isDebugEnabled())
        log.debugf("ATS: reflush jsapi ticket send: %s", url);

    Response resp = Http.get(url);
    if (!resp.isOK())
        throw new IllegalArgumentException("reflushJsapiTicket FAIL , openid=" + openid);
    String str = resp.getContent();

    if (log.isDebugEnabled())
        log.debugf("ATS: reflush jsapi ticket done: %s", str);

    NutMap re = Json.fromJson(NutMap.class, str);
    String ticket = re.getString("ticket");
    int expires = re.getInt("expires_in") - 200;//微信默认超时为7200秒,此处设置稍微短一点
    jsapiTicketStore.save(ticket, expires, System.currentTimeMillis());
}
 
Example 5
Source File: AbstractWxApi2.java    From nutzwx with Apache License 2.0 6 votes vote down vote up
protected void reflushCardTicket() {
    String at = this.getAccessToken();
    String url = String.format("%s/ticket/getticket?access_token=%s&type=wx_card", base, at);
    if (log.isDebugEnabled()) {
    	log.debugf("ATS: reflush wx_card ticket send: %s", url);
    }

    Response resp = Http.get(url);
    if (!resp.isOK()) {
        throw new IllegalArgumentException("reflushCardTicket FAIL , openid=" + openid);
    }
    String str = resp.getContent();

    if (log.isDebugEnabled()) {
        log.debugf("ATS: reflush wx_card ticket done: %s", str);
    }

    NutMap re = Json.fromJson(NutMap.class, str);
    String ticket = re.getString("ticket");
    int expires = re.getInt("expires_in") - 200;//微信默认超时为7200秒,此处设置稍微短一点
    cardTicketStore.save(ticket, expires, System.currentTimeMillis());
}
 
Example 6
Source File: AbstractWxApi2.java    From nutzwx with Apache License 2.0 6 votes vote down vote up
protected synchronized void reflushAccessToken() {
      String url = String.format("%s/token?grant_type=client_credential&appid=%s&secret=%s", base, appid, appsecret);
      if (log.isDebugEnabled())
          log.debugf("ATS: reflush access_token send: %s", url);

      Response resp = Http.get(url);
      if (!resp.isOK())
          throw new IllegalArgumentException("reflushAccessToken FAIL , openid=" + openid);
      String str = resp.getContent();

      if (log.isDebugEnabled())
          log.debugf("ATS: reflush access_token done: %s", str);

      NutMap re = Json.fromJson(NutMap.class, str);
if(re.getInt("errcode", 0)!=0)
	throw new IllegalArgumentException("reflushAccessToken FAIL : " + str);
      String token = re.getString("access_token");
      int expires = re.getInt("expires_in") - 200;//微信默认超时为7200秒,此处设置稍微短一点
      accessTokenStore.save(token, expires, System.currentTimeMillis());
  }
 
Example 7
Source File: WxApiImpl.java    From nutzwx with Apache License 2.0 6 votes vote down vote up
public void reflushAccessToken() {
    String url = String.format("%s/token?grant_type=client_credential&appid=%s&secret=%s",
                               base,
                               master.getAppid(),
                               master.getAppsecret());
    Response resp = Http.get(url);
    if (!resp.isOK())
        throw new IllegalArgumentException("reflushAccessToken FAIL , openid="
                                           + master.getOpenid());
    String str = resp.getContent();
    Map<String, Object> map = (Map<String, Object>) Json.fromJson(str);
    master.setAccess_token(map.get("token").toString());
    master.setAccess_token_expires(System.currentTimeMillis()
                                   + (((Number) map.get("expires_in")).intValue() - 60)
                                   * 1000);
}
 
Example 8
Source File: HttpTool.java    From mpsdk4j with Apache License 2.0 5 votes vote down vote up
public static String post(String url, String body) {
    if (log.isDebugEnabled()) {
        log.debugf("Request url: %s, post data: %s, default timeout: %d",
                   url,
                   body,
                   CONNECT_TIME_OUT);
    }

    try {
        Request req = Request.create(url, METHOD.POST);
        req.setEnc("UTF-8");
        req.setData(body);
        Response resp = Sender.create(req, CONNECT_TIME_OUT).send();
        if (resp.isOK()) {
            String content = resp.getContent();
            if (log.isInfoEnabled()) {
                log.infof("POST Request success. Response content: %s", content);
            }
            return content;
        }

        throw Lang.wrapThrow(new RuntimeException(String.format("Post request [%s] failed. status: %d",
                                                                url,
                                                                resp.getStatus())));
    }
    catch (Exception e) {
        throw Lang.wrapThrow(e);
    }
}
 
Example 9
Source File: TaobaoIP.java    From BigDataPlatform with GNU General Public License v3.0 4 votes vote down vote up
public static TaobaoIPResult getResult(String ip) {
  Response response = Http.get("http://ip.taobao.com/service/getIpInfo.php?ip=" + ip);
  TaobaoIPResult result = new TaobaoIPResult();
  if (ip != null && response.getStatus() == 200) {
    try {
      String content = response.getContent();
      Map<String, Object> contentMap = (Map)Json.fromJson(content);
      if (((Integer)((Integer)contentMap.get("code"))).intValue() == 0) {
        Map<String, Object> dataMap = (Map)contentMap.get("data");
        result.setCountry((String)dataMap.get("country"));
        result.setRegion((String)dataMap.get("region"));
        result.setCity((String)dataMap.get("city"));
        result.setCounty((String)dataMap.get("county"));
        result.setIsp((String)dataMap.get("isp"));
        result.setArea((String)dataMap.get("area"));
        result.setIp((String)dataMap.get("ip"));
        result.setCode(0);

        if (result.getCity().equals("内网IP") && result.getIsp().equals("内网IP")){
          result.setCode(0);
          result.setCountry("中国");
          result.setRegion("广东");
          result.setCity("东莞");
          result.setCounty("XX");
          result.setIsp("casc");
          result.setArea("XX");
          result.setIp(ip);
        }

        return result;
      }
    } catch (Exception var6) {
    }
  }

    result.setCode(-1);
    result.setCountry("XX");
    result.setRegion("XX");
    result.setCity("XX");
    result.setCounty("XX");
    result.setIsp("XX");
    result.setArea("XX");
    result.setIp(ip);
  return result;
}
 
Example 10
Source File: HttpTool.java    From mpsdk4j with Apache License 2.0 4 votes vote down vote up
public static Object download(String url) {
    if (log.isDebugEnabled()) {
        log.debugf("Download url: %s, default timeout: %d", url, CONNECT_TIME_OUT);
    }

    try {
        Response resp = Http.get(url);
        if (resp.isOK()) {
            String cd = resp.getHeader().get("Content-disposition");
            if (log.isInfoEnabled()) {
                log.infof("Get download file info: %s", cd);
            }

            if (Lang.isEmpty(cd)) {
                return resp.getContent();
            }

            cd = cd.substring(cd.indexOf(FILE_NAME_FLAG) + FILE_NAME_FLAG.length());
            String tmp = cd.startsWith("\"") ? cd.substring(1) : cd;
            tmp = tmp.endsWith("\"") ? cd.replace("\"", "") : cd;
            String filename = tmp.substring(0, tmp.lastIndexOf("."));
            String fileext = tmp.substring(tmp.lastIndexOf("."));
            if (log.isInfoEnabled()) {
                log.infof("Download file name: %s", filename);
                log.infof("Download file ext: %s", fileext);
            }

            File tmpfile = File.createTempFile(filename, fileext);
            InputStream is = resp.getStream();
            OutputStream os = new FileOutputStream(tmpfile);
            Streams.writeAndClose(os, is);
            return tmpfile;
        }

        throw Lang.wrapThrow(new RuntimeException(String.format("Download file [%s] failed. status: %d, content: %s",
                                                                url,
                                                                resp.getStatus(),
                                                                resp.getContent())));
    }
    catch (Exception e) {
        throw Lang.wrapThrow(e);
    }
}