Search in sources :

Example 41 with HttpEntityEnclosingRequest

use of org.apache.http.HttpEntityEnclosingRequest in project HongsCORE by ihongs.

the class Remote method request.

/**
 * 简单远程请求
 *
 * @param type
 * @param url
 * @param data
 * @param head
 * @param multipart
 * @return
 * @throws HongsException
 */
public static String request(METHOD type, String url, Map<String, Object> data, Map<String, String> head, boolean multipart) throws HongsException {
    if (url == null) {
        throw new NullPointerException("Request url can not be null");
    }
    HttpRequestBase http = null;
    try {
        // 构建 HTTP 请求对象
        switch(type) {
            case DELETE:
                http = new HttpDelete();
                break;
            case PATCH:
                http = new HttpPatch();
                break;
            case POST:
                http = new HttpPost();
                break;
            case PUT:
                http = new HttpPut();
                break;
            default:
                http = new HttpGet();
                break;
        }
        // 设置报文
        if (data != null) {
            if (http instanceof HttpEntityEnclosingRequest) {
                HttpEntityEnclosingRequest htte = (HttpEntityEnclosingRequest) http;
                if (multipart) {
                    htte.setEntity(buildPart(data));
                } else {
                    htte.setEntity(buildPost(data));
                }
            } else {
                String qry = EntityUtils.toString(buildPost(data), "UTF-8");
                if (url.indexOf('?') == -1) {
                    url += "?" + qry;
                } else {
                    url += "&" + qry;
                }
            }
        }
        // 设置报头
        if (head != null) {
            for (Map.Entry<String, String> et : head.entrySet()) {
                http.setHeader(et.getKey(), et.getValue());
            }
        }
        // 执行请求
        http.setURI(new URI(url));
        HttpResponse rsp = HttpClients.createDefault().execute(http);
        // 判断结果
        int sta = rsp.getStatusLine().getStatusCode();
        if (sta >= 300 && sta <= 399) {
            Header hea = rsp.getFirstHeader("Location");
            String loc = hea != null ? hea.getValue() : "";
            throw new StatusException(sta, url, loc);
        } else if (sta <= 199 || sta >= 400) {
            String txt = EntityUtils.toString(rsp.getEntity(), "UTF-8").trim();
            throw new StatusException(sta, url, txt);
        } else {
            String txt = EntityUtils.toString(rsp.getEntity(), "UTF-8").trim();
            return txt;
        }
    } catch (URISyntaxException | IOException ex) {
        throw new SimpleException(url, ex);
    } finally {
        if (http != null) {
            http.releaseConnection();
        }
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) HttpPatch(org.apache.http.client.methods.HttpPatch) HttpPut(org.apache.http.client.methods.HttpPut) Header(org.apache.http.Header) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)41 HttpEntity (org.apache.http.HttpEntity)24 HttpResponse (org.apache.http.HttpResponse)15 Header (org.apache.http.Header)12 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)12 HttpRequest (org.apache.http.HttpRequest)11 ProtocolVersion (org.apache.http.ProtocolVersion)11 URI (java.net.URI)8 IOException (java.io.IOException)7 HttpPost (org.apache.http.client.methods.HttpPost)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 HttpPut (org.apache.http.client.methods.HttpPut)5 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)5 RequestWrapper (org.apache.http.impl.client.RequestWrapper)5 Iterator (java.util.Iterator)4 HttpException (org.apache.http.HttpException)4 HttpHost (org.apache.http.HttpHost)4 ProtocolException (java.net.ProtocolException)3 SocketTimeoutException (java.net.SocketTimeoutException)3 ProtocolException (org.apache.http.ProtocolException)3