top.jfunc.http.component.apache.ApacheHttpRequestExecutor Maven / Gradle / Ivy
package top.jfunc.http.component.apache;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.protocol.HttpClientContext;
import top.jfunc.http.base.ContentCallback;
import top.jfunc.http.component.*;
import top.jfunc.http.request.HttpRequest;
import top.jfunc.http.response.ClientHttpResponse;
import java.io.IOException;
/**
* Apache实现的处理
* @author xiongshiyan
* @since 2020.12.01
* @since 1.2.12
*/
public class ApacheHttpRequestExecutor extends BaseHttpRequestExecutor implements HttpRequestExecutor {
private RequesterFactory httpRequestRequesterFactory;
private RequesterFactory httpClientRequesterFactory;
public ApacheHttpRequestExecutor() {
super(new DefaultApacheHeaderHandler(), new DefaultApacheResponseStreamExtractor(), new DefaultApacheHeaderExtractor());
this.httpRequestRequesterFactory = new DefaultApacheRequestFactory();
this.httpClientRequesterFactory = new DefaultApacheClientFactory();
}
public ApacheHttpRequestExecutor(HeaderHandler httpUriRequestHeaderHandler,
StreamExtractor responseStreamExtractor,
HeaderExtractor responseHeaderExtractor,
RequesterFactory httpRequestRequesterFactory,
RequesterFactory httpClientRequesterFactory) {
super(httpUriRequestHeaderHandler, responseStreamExtractor, responseHeaderExtractor);
this.httpRequestRequesterFactory = httpRequestRequesterFactory;
this.httpClientRequesterFactory = httpClientRequesterFactory;
}
@Override
public ClientHttpResponse execute(HttpRequest httpRequest, ContentCallback contentCallback) throws IOException{
//1.创建并配置
org.apache.http.HttpRequest request = getHttpRequestRequesterFactory().create(httpRequest);
//2.创建请求内容,如果有的话
handleBody(request , contentCallback , httpRequest);
//3.设置header
handleHeaders(request, httpRequest);
//4.创建HttpClient
HttpClient httpClient = getHttpClientRequesterFactory().create(httpRequest);
//5.发送请求
HttpResponse response = getResponse(httpClient, request , httpRequest);
return new ApacheClientHttpResponse(httpClient, response, httpRequest, getResponseStreamExtractor(), getResponseHeaderExtractor());
}
protected HttpResponse getResponse(HttpClient httpClient, org.apache.http.HttpRequest request , HttpRequest httpRequest) throws IOException {
return httpClient.execute((HttpUriRequest) request , HttpClientContext.create());
}
public RequesterFactory getHttpRequestRequesterFactory() {
return httpRequestRequesterFactory;
}
public RequesterFactory getHttpClientRequesterFactory() {
return httpClientRequesterFactory;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy