org.nutz.http.Http Java Examples

The following examples show how to use org.nutz.http.Http. 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: AddressUtils.java    From NutzSite with Apache License 2.0 6 votes vote down vote up
public static String getRealAddressByIP(String ip) {
    String address = "XX XX";
    // 内网不查询
    if (IpUtils.internalIp(ip)) {
        return "内网IP";
    }
    try {
        Response rspStr = Http.get(IP_URL + "?ip=" + ip, 5 * 1000);
        if (!rspStr.isOK()) {
            log.error("获取地理位置异常 {}", ip);
            return address;
        }
        JSONObject obj = JSONObject.parseObject(rspStr.getContent());
        JSONObject data = obj.getObject("data", JSONObject.class);
        String region = data.getString("region");
        String city = data.getString("city");
        address = region + " " + city;
    } catch (Exception e) {
        log.error("IP查询失败:" + ip + e.getMessage());
    }
    return address;
}
 
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: 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 #4
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 #5
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 #6
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 #7
Source File: BankCardUtils.java    From NutzSite with Apache License 2.0 5 votes vote down vote up
/**
 * 支付宝 验证银行卡
 * @param cardId
 * @return
 */
public static CardInfoData validateAndCacheCardInfo(String cardId){
    cardId = cardId.replaceAll(" ", "");
    String url = "https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardBinCheck=true&cardNo="+ cardId;
    String res = Http.get(url,5000).getContent();
    System.out.println(res);
    CardInfoData cardInfoData = new CardInfoData();
    if(Strings.isNotBlank(res)){
        cardInfoData = Json.fromJson(CardInfoData.class, res);
    }
    return cardInfoData;
}
 
Example #8
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 #9
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);
    }
}