import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.stream.Collectors;

/**
 * Unirest 轻量级
 *
 * @author wangjin
 * https://mvnrepository.com/artifact/com.mashape.unirest/unirest-java
 * <dependency>
 * <groupId>com.mashape.unirest</groupId>
 * <artifactId>unirest-java</artifactId>
 * <version>1.4.9</version>
 * </dependency>
 */
public class Unirest {

    public static HttpRequest<GetRequest> get(String url) throws Exception {
        return new GetRequest(url);
    }

    public static PostRequest post(String url) throws Exception {
        return new PostRequest(url);
    }

    public interface HttpRequest<R extends HttpRequest<R>> {
        R header(String name, String value);

        HttpResponse<String> asString();
    }

    public static interface HttpResponse<T> {
        int getStatus() throws IOException;

        T getBody() throws IOException;
    }

    public static abstract class BaseRequest<R extends HttpRequest<R>> implements HttpRequest<R> {

        final ThreadLocal<HttpURLConnection> HTTP_URL_CONNECTION_THREAD_LOCAL = new ThreadLocal<>();

        public BaseRequest(String url, String method) throws Exception {
            HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(url).openConnection();
            httpURLConnection.setRequestMethod(method);
            HTTP_URL_CONNECTION_THREAD_LOCAL.set(httpURLConnection);
        }

        @Override
        public R header(String name, String value) {
            HTTP_URL_CONNECTION_THREAD_LOCAL.get().setRequestProperty(name, value);
            return (R) this;
        }

        @Override
        public HttpResponse<String> asString() {
            try {
                HttpURLConnection connection = HTTP_URL_CONNECTION_THREAD_LOCAL.get();
                return new HttpResponse<String>() {
                    @Override
                    public int getStatus() throws IOException {
                        return connection.getResponseCode();
                    }

                    @Override
                    public String getBody() throws IOException {
                        return new BufferedReader(new InputStreamReader(connection.getInputStream())).lines().collect(Collectors.joining());
                    }
                };
            } finally {
                HTTP_URL_CONNECTION_THREAD_LOCAL.remove();
            }
        }
    }

    public static class GetRequest extends BaseRequest<GetRequest> {
        public GetRequest(String url) throws Exception {
            super(url, "GET");
        }
    }

    public static class PostRequest extends BaseRequest<PostRequest> {
        public PostRequest(String url) throws Exception {
            super(url, "POST");
        }

        public PostRequest body(String body) throws Exception {
            HttpURLConnection httpURLConnection = HTTP_URL_CONNECTION_THREAD_LOCAL.get();
            // 发送POST请求必须设置允许输出
            httpURLConnection.setDoOutput(true);
            // 发送POST请求必须设置允许输入
            httpURLConnection.setDoInput(true);
            OutputStream os = httpURLConnection.getOutputStream();
            os.write(body.getBytes());
            os.flush();
            os.close();
            return this;
        }
    }
}

打赏
支付宝 微信
上一篇 下一篇