Search in sources :

Example 1 with WechatRequest

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");
    }
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) Request(org.apache.http.client.fluent.Request) WechatRequest(org.usc.wechat.mp.sdk.vo.WechatRequest) URI(java.net.URI) ClientProtocolException(org.apache.http.client.ClientProtocolException) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 URI (java.net.URI)1 NameValuePair (org.apache.http.NameValuePair)1 ClientProtocolException (org.apache.http.client.ClientProtocolException)1 HttpResponseException (org.apache.http.client.HttpResponseException)1 Request (org.apache.http.client.fluent.Request)1 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)1 WechatRequest (org.usc.wechat.mp.sdk.vo.WechatRequest)1