use of org.usc.wechat.mp.sdk.vo.WechatRequest in project wechat-mp-sdk by usc.
the class HttpUtil method postBodyRequest.
public static <T extends JsonRtn> T postBodyRequest(WechatRequest request, License license, Map<String, String> paramMap, Object requestBody, Class<T> jsonRtnClazz) {
if (request == null || license == null || jsonRtnClazz == null) {
return JsonRtnUtil.buildFailureJsonRtn(jsonRtnClazz, "missing post request params");
}
String requestUrl = request.getUrl();
String requestName = request.getName();
List<NameValuePair> form = buildNameValuePairs(paramMap);
String body = requestBody != null ? JSONObject.toJSONString(requestBody) : StringUtils.EMPTY;
URI uri = buildURI(requestUrl, buildNameValuePairs(license));
if (uri == null) {
return JsonRtnUtil.buildFailureJsonRtn(jsonRtnClazz, "build request URI failed");
}
try {
Request post = Request.Post(uri).connectTimeout(CONNECT_TIMEOUT).socketTimeout(SOCKET_TIMEOUT);
if (paramMap != null) {
post.bodyForm(form);
}
if (StringUtils.isNotEmpty(body)) {
post.bodyString(body, ContentType.create("text/html", Consts.UTF_8));
}
String rtnJson = post.execute().handleResponse(HttpUtil.UTF8_CONTENT_HANDLER);
T jsonRtn = JsonRtnUtil.parseJsonRtn(rtnJson, jsonRtnClazz);
log.info(requestName + " result:\n url={},\n form={},\n body={},\n rtn={},\n {}", uri, form, body, rtnJson, jsonRtn);
return jsonRtn;
} catch (Exception e) {
String msg = requestName + " failed:\n url=" + uri + ",\n form=" + form + ",\n body=" + body;
log.error(msg, e);
return JsonRtnUtil.buildFailureJsonRtn(jsonRtnClazz, "post request server failed");
}
}
Aggregations