1818
1919import static com .flow .platform .util .http .HttpResponse .EXCEPTION_STATUS_CODE ;
2020
21+ import com .flow .platform .util .StringUtil ;
2122import java .io .IOException ;
2223import java .io .InputStream ;
2324import java .io .UnsupportedEncodingException ;
2930import java .util .function .Consumer ;
3031import org .apache .commons .codec .binary .Base64 ;
3132import org .apache .http .HttpEntity ;
33+ import org .apache .http .client .config .RequestConfig ;
3234import org .apache .http .client .methods .CloseableHttpResponse ;
3335import org .apache .http .client .methods .HttpGet ;
3436import org .apache .http .client .methods .HttpPost ;
3537import org .apache .http .client .methods .HttpRequestBase ;
3638import org .apache .http .entity .ContentType ;
3739import org .apache .http .entity .StringEntity ;
3840import org .apache .http .impl .client .CloseableHttpClient ;
39- import org .apache .http .impl .client .HttpClients ;
41+ import org .apache .http .impl .client .HttpClientBuilder ;
4042import org .apache .http .util .EntityUtils ;
4143
4244/**
@@ -63,6 +65,14 @@ public static HttpClient build(String url) {
6365 return new HttpClient (url );
6466 }
6567
68+ private final static int HTTP_TIMEOUT = 5 * 1000 ;
69+
70+ private final RequestConfig config = RequestConfig .custom ()
71+ .setConnectTimeout (HTTP_TIMEOUT )
72+ .setConnectionRequestTimeout (HTTP_TIMEOUT )
73+ .setSocketTimeout (HTTP_TIMEOUT )
74+ .build ();
75+
6676 private final String url ;
6777
6878 private HttpRequestBase httpRequest ;
@@ -142,7 +152,7 @@ public HttpResponse<String> bodyAsString() {
142152
143153 exec (httpResponse -> {
144154 if (httpResponse == null ) {
145- wrapper .add (new HttpResponse <>(retried , EXCEPTION_STATUS_CODE , exceptions , "" ));
155+ wrapper .add (new HttpResponse <>(retried , EXCEPTION_STATUS_CODE , exceptions , StringUtil . EMPTY ));
146156 return ;
147157 }
148158
@@ -152,7 +162,7 @@ public HttpResponse<String> bodyAsString() {
152162 wrapper .add (new HttpResponse <>(retried , statusCode , exceptions , body ));
153163 } catch (IOException e ) {
154164 exceptions .add (e );
155- wrapper .add (new HttpResponse <>(retried , EXCEPTION_STATUS_CODE , exceptions , "" ));
165+ wrapper .add (new HttpResponse <>(retried , EXCEPTION_STATUS_CODE , exceptions , StringUtil . EMPTY ));
156166 }
157167 });
158168
@@ -186,7 +196,7 @@ private void exec(Consumer<CloseableHttpResponse> consumer) {
186196 return ;
187197 }
188198
189- try (CloseableHttpClient httpClient = HttpClients . createDefault ()) {
199+ try (CloseableHttpClient httpClient = HttpClientBuilder . create (). setDefaultRequestConfig ( config ). build ()) {
190200 try (CloseableHttpResponse response = httpClient .execute (httpRequest )) {
191201 int statusCode = response .getStatusLine ().getStatusCode ();
192202
0 commit comments