All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.net.wanmo.common.http.apache.HttpResult Maven / Gradle / Ivy

There is a newer version: 1.0.4
Show newest version
package cn.net.wanmo.common.http.apache;

import org.apache.http.*;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.*;

public class HttpResult {
    private HttpResponse response;
    private Integer statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR;
    private String message = "";

    public HttpResult() {
    }

    public HttpResponse getResponse() {
        return response;
    }

    public void setResponse(HttpResponse response) {
        this.response = response;
        this.statusCode = response.getStatusLine().getStatusCode();
    }

    public Integer getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(Integer statusCode) {
        this.statusCode = statusCode;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    // ======================================================================
    public String body() {
        String s = "";
        try {
            if (Objects.isNull(this.response)) {
                return s;
            }

            HttpEntity entity = response.getEntity();
            if (Objects.isNull(entity)) {
                return s;
            }

            {
                s = EntityUtils.toString(entity, Consts.UTF_8);
                EntityUtils.consume(entity);
            }

            return s;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public Map> headers() {
        Map> headers = new HashMap<>();
        if (Objects.isNull(this.response)) {
            return headers;
        }

        Header[] allHeaders = this.response.getAllHeaders();
        for (Header header : allHeaders) {
            headers.put(header.getName(), Arrays.asList(header.getValue()));
        }

        return headers;
    }

    public boolean isOk() {
        return HttpStatus.SC_OK == this.statusCode;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy