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

top.jfunc.http.smart.JdkSmartHttpClient Maven / Gradle / Ivy

package top.jfunc.http.smart;

import top.jfunc.http.component.*;
import top.jfunc.http.component.jdk.DefaultJdkBodyContentCallbackCreator;
import top.jfunc.http.component.jdk.DefaultJdkUploadContentCallbackCreator;
import top.jfunc.http.component.jdk.JdkHttpRequestExecutor;

import java.net.HttpURLConnection;

/**
 * 使用URLConnection实现的Http请求类
 * @author 熊诗言2020/12/01
 */
public class JdkSmartHttpClient extends AbstractImplementSmartHttpClient {

    public JdkSmartHttpClient(){
        super(new DefaultJdkBodyContentCallbackCreator(),
                new DefaultJdkUploadContentCallbackCreator(),
                new JdkHttpRequestExecutor());
    }

    public JdkSmartHttpClient(ContentCallbackCreator bodyContentCallbackCreator,
                              ContentCallbackCreator uploadContentCallbackCreator,
                              HttpRequestExecutor httpRequestExecutor) {
        super(bodyContentCallbackCreator, uploadContentCallbackCreator, httpRequestExecutor);
    }

    public JdkSmartHttpClient(AssemblingFactory assemblingFactory,
                              ContentCallbackCreator bodyContentCallbackCreator,
                              ContentCallbackCreator uploadContentCallbackCreator,
                              HttpRequestExecutor httpRequestExecutor) {
        super(assemblingFactory, bodyContentCallbackCreator, uploadContentCallbackCreator, httpRequestExecutor);
    }
    ///
    /*
    @Override
    protected  R doInternalTemplate(HttpRequest httpRequest, ContentCallback contentCallback , ResultCallback resultCallback) throws Exception {
        ClientHttpResponse clientHttpResponse= null;
        try {
            clientHttpResponse = exe(httpRequest, contentCallback);

            //jdk对于重定向的特殊处理
            MultiValueMap responseHeaders = clientHttpResponse.getHeaders();
            if(needRedirect(httpRequest, clientHttpResponse.getStatusCode(), responseHeaders)){
                String redirectUrl = responseHeaders.getFirst(HttpHeaders.LOCATION);
                HttpRequest hr = createRedirectHttpRequest(httpRequest, redirectUrl);
                return doInternalTemplate(hr, null, resultCallback);
            }

            return resultCallback.convert(clientHttpResponse.getStatusCode(), clientHttpResponse.getBody(), calculateResultCharset(httpRequest), responseHeaders);
        } finally {
            IoUtil.close(clientHttpResponse);
        }
    }
    protected HttpRequest createRedirectHttpRequest(HttpRequest httpRequest, String redirectUrl) {
        HttpRequest hr = getAssemblingFactory().create(redirectUrl , null , null , httpRequest.getConnectionTimeout() , httpRequest.getReadTimeout() , httpRequest.getResultCharset());
        init(hr , Method.GET);
        //处理多次重定向的情况
        hr.followRedirects(Config.FOLLOW_REDIRECTS);
        return hr;
    }
    protected boolean needRedirect(HttpRequest httpRequest, int statusCode, MultiValueMap responseHeaders) {
        Config config = httpRequest.getConfig();
        boolean followRedirects = ObjectUtil.defaultIfNull(httpRequest.followRedirects() , config.followRedirects());
        return followRedirects && HttpStatus.needRedirect(statusCode)
                && MapUtil.notEmpty(responseHeaders)
                && responseHeaders.containsKey(HttpHeaders.LOCATION);
    }*/

    @Override
    public String toString() {
        return "SmartHttpClient implemented by JDK's HttpURLConnection";
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy