Skip to content

WbpHttpClientRequest

echisan edited this page Apr 5, 2019 · 2 revisions

这是最早时候用HttpClient去实现WbpHttpRequest接口的类
后面为了减少依赖而去掉了,实际上应该也能用的,不过需要修改点东西
如果要设计成线程安全的,hearder那个地方应该修改一下
这里主要有这个东西主要是当时想当个备份而已

package cn.echisan.wbp4j.http;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by echisan on 2018/11/6
 */
public class WbpHttpClientRequest implements WbpHttpRequest {
    private HttpClientBuilder httpClientBuilder;
    private Map<String, String> header;

    public WbpHttpClientRequest() {
        this.httpClientBuilder = HttpClientBuilder.create();
    }

    @Override
    public void setHeader(Map<String, String> header) {
        this.header = header;
    }

    @Override
    public Map<String, String> getHeader() {
        return this.header;
    }

    @Override
    public WbpHttpResponse doGet(String url, Map<String, String> header, Map<String, String> params) throws IOException {
        CloseableHttpClient client = httpClientBuilder.build();
        if (params != null) {
            url = url + "?" + convertParams(params);
        }
        HttpGet httpGet = new HttpGet(url);

        if (header != null) {
            header.forEach(httpGet::setHeader);
        }

        return doRequest(client, httpGet);
    }

    @Override
    public WbpHttpResponse doGet(String url, Map<String, String> params) throws IOException {
        return doGet(url, this.header, params);
    }

    @Override
    public WbpHttpResponse doGet(String url) throws IOException {
        return doGet(url, null, null);
    }

    @Override
    public WbpHttpResponse doPost(String url, Map<String, String> header, Map<String, String> params) throws IOException {
        CloseableHttpClient client = httpClientBuilder.build();
        HttpPost httpPost = new HttpPost(url);
        if (header != null) {
            header.forEach(httpPost::setHeader);
        }

        if (params != null) {
            HttpEntity httpEntity = new StringEntity(convertParams(params), ContentType.APPLICATION_FORM_URLENCODED);
            httpPost.setEntity(httpEntity);
        }

        return doRequest(client, httpPost);
    }

    @Override
    public WbpHttpResponse doPost(String url, Map<String, String> params) throws IOException {
        return doPost(url, this.header, params);
    }

    @Override
    public WbpHttpResponse doPost(String url) throws IOException {
        return doPost(url, this.header, null);
    }

    @Override
    public WbpHttpResponse doPostMultiPart(String url, Map<String, String> header, String content) throws IOException {
        CloseableHttpClient client = httpClientBuilder.build();
        HttpPost httpPost = new HttpPost(url);
        if (header != null) {
            header.forEach(httpPost::setHeader);
        }

        MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
        StringBody stringBody = new StringBody(content, ContentType.MULTIPART_FORM_DATA);
        multipartEntityBuilder.addPart("b64_data", stringBody);
        httpPost.setEntity(multipartEntityBuilder.build());

        return doRequest(client, httpPost);
    }

    private WbpHttpResponse doRequest(CloseableHttpClient httpClient, HttpUriRequest httpRequest) throws IOException {

        System.out.println(Arrays.toString(httpRequest.getAllHeaders()));

        CloseableHttpResponse response = httpClient.execute(httpRequest);
        Header[] allHeaders = response.getAllHeaders();
        int statusCode = response.getStatusLine().getStatusCode();
        String body = null;
        HttpEntity responseEntity = response.getEntity();

        if (responseEntity.isStreaming()) {
            body = readBodyStream(responseEntity.getContent());
        }

        httpClient.close();
        response.close();
        return new DefaultWbpHttpResponse(statusCode, convertHeader(allHeaders), body);
    }

    private Map<String, String> convertHeader(Header[] headers) {
        Map<String, String> header = new HashMap<>();
        StringBuilder cookie = new StringBuilder();
        for (Header h : headers) {
            if (h.getName().equalsIgnoreCase("set-cookie")) {
                String s = h.getValue();
                int i = s.indexOf(";");
                if (i != -1) {
                    String substring = s.substring(0, i);
                    cookie.append(substring).append("; ");
                }
            }
            header.put(h.getName(), h.getValue());
        }
        if (cookie.length() > 1) {
            String c = cookie.toString().substring(0, cookie.length() - 2);
            header.put("set-cookie", c);
        }
        return header;
    }

    private String readBodyStream(InputStream is) throws IOException {
        StringBuilder sb = new StringBuilder();
        String temp;
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        while ((temp = br.readLine()) != null) {
            sb.append(temp);
        }
        return sb.toString();
    }
}
Clone this wiki locally