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

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

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

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

import java.io.IOException;
import java.util.Objects;

public class HttpResult {
    protected HttpResponse response;

    public HttpResult() {
    }

    public HttpResult(HttpResponse response) {
        this.response = response;
    }

    public HttpResponse getResponse() {
        return response;
    }

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

    public Integer statusCode() {
        return response.getStatusLine().getStatusCode();
    }

    public Header[] header() {
        return response.getAllHeaders();
    }


    public String body() {
        String s = "";
        try {
            HttpEntity entity = response.getEntity();

            if (Objects.nonNull(entity)) {
                s = EntityUtils.toString(entity, Consts.UTF_8);
                EntityUtils.consume(entity);

            }

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy