top.jfunc.http.component.jdk.JdkHttpRequestExecutor Maven / Gradle / Ivy
package top.jfunc.http.component.jdk;
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;
import java.net.HttpURLConnection;
/**
* HTTPURLConnection实现的处理
* @author xiongshiyan
* @since 2020.12.01
* @since 1.2.12
*/
public class JdkHttpRequestExecutor extends BaseHttpRequestExecutor implements HttpRequestExecutor {
private RequesterFactory httpURLConnectionFactory;
public JdkHttpRequestExecutor() {
super(new DefaultJdkHeaderHandler(), new DefaultJdkStreamExtractor(), new DefaultJdkHeaderExtractor());
this.httpURLConnectionFactory = new DefaultJdkConnectionFactory();
}
public JdkHttpRequestExecutor(HeaderHandler httpURLConnectionHeaderHandler,
StreamExtractor responseStreamExtractor,
HeaderExtractor responseHeaderExtractor,
RequesterFactory httpURLConnectionFactory) {
super(httpURLConnectionHeaderHandler, responseStreamExtractor, responseHeaderExtractor);
this.httpURLConnectionFactory = httpURLConnectionFactory;
}
@Override
public ClientHttpResponse execute(HttpRequest httpRequest, ContentCallback contentCallback) throws IOException{
//1.初始化connection
HttpURLConnection connection = getHttpURLConnectionFactory().create(httpRequest);
//2.处理header[必须在写入body之前就设置好header]
handleHeaders(connection, httpRequest);
//3.写入内容
handleBody(connection , contentCallback , httpRequest);
//4.连接
getResponse(connection, httpRequest);
return new JdkClientHttpResponse(connection, httpRequest, getResponseStreamExtractor(), getResponseHeaderExtractor());
}
protected HttpURLConnection getResponse(HttpURLConnection connection, HttpRequest httpRequest) throws IOException {
connection.connect();
return connection;
}
public RequesterFactory getHttpURLConnectionFactory() {
return httpURLConnectionFactory;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy