Java Code Examples for org.nutz.http.Request#setData()
The following examples show how to use
org.nutz.http.Request#setData() .
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: WxApiImpl.java From nutzwx with Apache License 2.0 | 6 votes |
protected Map<String, Object> call(String URL, METHOD method, String body) { String token = getAccessToken(); if (URL.contains("?")) { URL = base + URL + "&access_token=" + token; } else { URL = base + URL + "?access_token=" + token; } Request req = Request.create(URL, method); if (body != null) req.setData(body); Response resp = Sender.create(req).send(); if (!resp.isOK()) throw new IllegalArgumentException("resp code=" + resp.getStatus()); Map<String, Object> map = (Map<String, Object>) Json.fromJson(resp.getReader()); if (map != null && map.containsKey("errcode") && ((Number) map.get("errcode")).intValue() != 0) { throw new IllegalArgumentException(map.toString()); } return map; }
Example 2
Source File: WxApi2Impl.java From nutzwx with Apache License 2.0 | 6 votes |
/** * 微信支付公共POST方法(带证书) * * @param url * 请求路径 * @param key * 商户KEY * @param params * 参数 * @param file * 证书文件 * @param password * 证书密码 * @return */ @Override public NutMap postPay(String url, String key, Map<String, Object> params, Object data, String password) { params.remove("sign"); String sign = WxPaySign.createSign(key, params); params.put("sign", sign); Request req = Request.create(url, METHOD.POST); req.setData(Xmls.mapToXml(params)); Sender sender = Sender.create(req); SSLSocketFactory sslSocketFactory; try { sslSocketFactory = WxPaySSL.buildSSL(data, password); } catch (Exception e) { throw Lang.wrapThrow(e); } sender.setSSLSocketFactory(sslSocketFactory); Response resp = sender.send(); if (!resp.isOK()) throw new IllegalStateException("postPay with SSL, resp code=" + resp.getStatus()); return Xmls.xmlToMap(resp.getContent("UTF-8")); }
Example 3
Source File: LoachClient.java From nutzcloud with Apache License 2.0 | 5 votes |
protected boolean _reg(String regData) { try { String regURL = url + "/reg"; if (isDebug()) { log.debug("Reg URL :" + regURL); log.debug("Reg Data:" + regData); } Request req = Request.create(regURL, METHOD.POST); req.setData(regData); req.getHeader().clear(); req.getHeader().asJsonContentType(); Response resp = Sender.create(req).setTimeout(3000).send(); if (resp.isOK()) { NutMap re = Json.fromJson(NutMap.class, resp.getReader()); if (re != null && re.getBoolean("ok", false)) { log.infof("Reg Done id=%s url=%s", id, url); regOk = true; return true; } else if (re == null) { log.info("Reg Err, revc NULL"); return false; } else { log.info("Reg Err " + re); return false; } } } catch (Throwable e) { log.debugf("bad url? %s %s", url, e.getMessage()); } return false; }
Example 4
Source File: HttpTool.java From mpsdk4j with Apache License 2.0 | 5 votes |
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 5
Source File: WxApi2Impl.java From nutzwx with Apache License 2.0 | 5 votes |
@Override public NutResource get_material(String media_id) { String url = String.format("%s/cgi-bin/material/get_material?access_token=%s", wxBase, getAccessToken()); Request req = Request.create(url, METHOD.POST); NutMap body = new NutMap(); body.put("media_id", media_id); req.setData(Json.toJson(body)); final Response resp = Sender.create(req).send(); if (!resp.isOK()) throw new IllegalStateException("download media file, resp code=" + resp.getStatus()); String disposition = resp.getHeader().get("Content-disposition"); return new WxResource(disposition, resp.getStream()); }
Example 6
Source File: WxApi2Impl.java From nutzwx with Apache License 2.0 | 5 votes |
/** * 微信支付公共POST方法(不带证书) * * @param url * 请求路径 * @param key * 商户KEY * @param params * 参数 * @return */ @Override public NutMap postPay(String url, String key, Map<String, Object> params) { params.remove("sign"); String sign = WxPaySign.createSign(key, params); params.put("sign", sign); Request req = Request.create(url, METHOD.POST); req.setData(Xmls.mapToXml(params)); Response resp = Sender.create(req).send(); if (!resp.isOK()) throw new IllegalStateException("postPay, resp code=" + resp.getStatus()); return Xmls.xmlToMap(resp.getContent("UTF-8")); }
Example 7
Source File: AbstractWxApi2.java From nutzwx with Apache License 2.0 | 4 votes |
protected WxResp call(String URL, METHOD method, String body) { String token = getAccessToken(); if (log.isInfoEnabled()) { log.info("wxapi call: " + URL); if (log.isDebugEnabled()) { log.debug(body); } } int retry = retryTimes; WxResp wxResp = null; while (retry >= 0) { try { String sendUrl = URL.startsWith("http") ? URL : base + URL; if (URL.contains("?")) { sendUrl += "&access_token=" + token; } else { sendUrl += "?access_token=" + token; } Request req = Request.create(sendUrl, method); if (body != null) req.setData(body); Response resp = Sender.create(req).send(); if (!resp.isOK()) throw new IllegalArgumentException("resp code=" + resp.getStatus()); wxResp = Json.fromJson(WxResp.class, resp.getReader("UTF-8")); // 处理微信返回 40001 invalid credential if (wxResp.errcode() != 40001) { break;//正常直接返回 } else { log.warnf("wxapi of access_token request [%s] finished, but the return code is 40001, try to reflush access_token right now, surplus retry times : %s", URL, retry); // 强制刷新一次acess_token reflushAccessToken(); } } catch (Exception e) { if (retry >= 0) { log.warn("reflushing access_token... " + retry + " retries left.", e); } else { log.errorf("%s times attempts to get a wx access_token , but all failed!", retryTimes); throw Lang.wrapThrow(e); } } finally { retry--; } } return wxResp; }