@@ -112,7 +112,6 @@ public void setTimeout(int timeout)
112112 private String reasonLine = "" ;
113113 private HttpClientBuilder httpClientBuilder ;
114114 private HttpClientContext httpClientContext = null ;
115- private CloseableHttpClient httpClient = null ;
116115 private CloseableHttpResponse response = null ;
117116 private CredentialsProvider credentialsProvider = null ;
118117 private RequestConfig reqConfig = null ; // Atributo usado en la ejecucion del metodo (por ejemplo, httpGet, httpPost)
@@ -333,22 +332,24 @@ public void execute(String method, String url) {
333332 this .httpClientBuilder .setDefaultCookieStore (cookiesToSend ); // Cookies Seteo CookieStore
334333 }
335334
335+ int msTimeout = getTimeout () * 1000 ;
336+
337+ RequestConfig .Builder requestConfigBuilder = RequestConfig .custom ()
338+ .setCookieSpec (CookieSpecs .STANDARD )
339+ .setSocketTimeout (msTimeout )
340+ .setConnectionRequestTimeout (msTimeout )
341+ .setConnectTimeout (msTimeout );
342+
343+ this .httpClientBuilder .setRoutePlanner (null );
344+
336345 if (getProxyInfoChanged () && !getProxyServerHost ().isEmpty () && getProxyServerPort () != 0 ) {
337346 HttpHost proxy = new HttpHost (getProxyServerHost (), getProxyServerPort ());
338347 this .httpClientBuilder .setRoutePlanner (new DefaultProxyRoutePlanner (proxy ));
339- this .reqConfig = RequestConfig .custom ()
340- .setSocketTimeout (getTimeout () * 1000 ) // Se multiplica por 1000 ya que tiene que ir en ms y se recibe en segundos
341- .setCookieSpec (CookieSpecs .STANDARD )
342- .setProxy (proxy )
343- .build ();
344- } else {
345- this .httpClientBuilder .setRoutePlanner (null );
346- this .reqConfig = RequestConfig .custom ()
347- .setConnectTimeout (getTimeout () * 1000 ) // Se multiplica por 1000 ya que tiene que ir en ms y se recibe en segundos
348- .setCookieSpec (CookieSpecs .STANDARD )
349- .build ();
348+ requestConfigBuilder .setProxy (proxy );
350349 }
351350
351+ this .reqConfig = requestConfigBuilder .build ();
352+
352353 if (getHostChanged () || getAuthorizationChanged ()) { // Si el host cambio o si se agrego alguna credencial
353354 this .credentialsProvider = new BasicCredentialsProvider ();
354355
@@ -434,121 +435,120 @@ public void execute(String method, String url) {
434435 else
435436 url = url .startsWith ("http://" ) ? url : "http://" + getHost () + ":" + (getPort () == -1 ? "80" :getPort ()) + url ;
436437
437- httpClient = this .httpClientBuilder .build ();
438-
439- if (method .equalsIgnoreCase ("GET" )) {
440- HttpGetWithBody httpget = new HttpGetWithBody (url .trim ());
441- httpget .setConfig (reqConfig );
442- Set <String > keys = getheadersToSend ().keySet ();
443- for (String header : keys ) {
444- httpget .addHeader (header ,getheadersToSend ().get (header ));
445- }
438+ try (CloseableHttpClient httpClient = this .httpClientBuilder .build ()) {
439+ if (method .equalsIgnoreCase ("GET" )) {
440+ HttpGetWithBody httpget = new HttpGetWithBody (url .trim ());
441+ httpget .setConfig (reqConfig );
442+ Set <String > keys = getheadersToSend ().keySet ();
443+ for (String header : keys ) {
444+ httpget .addHeader (header , getheadersToSend ().get (header ));
445+ }
446446
447- httpget .setEntity (new ByteArrayEntity (getData ()));
447+ httpget .setEntity (new ByteArrayEntity (getData ()));
448448
449- response = httpClient .execute (httpget , httpClientContext );
449+ response = httpClient .execute (httpget , httpClientContext );
450450
451- } else if (method .equalsIgnoreCase ("POST" )) {
452- HttpPost httpPost = new HttpPost (url .trim ());
453- httpPost .setConfig (reqConfig );
454- Set <String > keys = getheadersToSend ().keySet ();
455- boolean hasConentType = false ;
456- for (String header : keys ) {
457- httpPost .addHeader (header ,getheadersToSend ().get (header ));
458- if (header .equalsIgnoreCase ("Content-type" ))
459- hasConentType = true ;
460- }
461- if (!hasConentType ) // Si no se setea Content-type, se pone uno default
462- httpPost .addHeader ("Content-type" , "application/x-www-form-urlencoded" );
463-
464- ByteArrayEntity dataToSend ;
465- if (!getIsMultipart () && getVariablesToSend ().size () > 0 )
466- dataToSend = new ByteArrayEntity (CommonUtil .hashtable2query (getVariablesToSend ()).getBytes ());
467- else
468- dataToSend = new ByteArrayEntity (getData ());
469- httpPost .setEntity (dataToSend );
470-
471- response = httpClient .execute (httpPost , httpClientContext );
472-
473- } else if (method .equalsIgnoreCase ("PUT" )) {
474- HttpPut httpPut = new HttpPut (url .trim ());
475- httpPut .setConfig (reqConfig );
476- Set <String > keys = getheadersToSend ().keySet ();
477- for (String header : keys ) {
478- httpPut .addHeader (header ,getheadersToSend ().get (header ));
479- }
451+ } else if (method .equalsIgnoreCase ("POST" )) {
452+ HttpPost httpPost = new HttpPost (url .trim ());
453+ httpPost .setConfig (reqConfig );
454+ Set <String > keys = getheadersToSend ().keySet ();
455+ boolean hasConentType = false ;
456+ for (String header : keys ) {
457+ httpPost .addHeader (header , getheadersToSend ().get (header ));
458+ if (header .equalsIgnoreCase ("Content-type" ))
459+ hasConentType = true ;
460+ }
461+ if (!hasConentType ) // Si no se setea Content-type, se pone uno default
462+ httpPost .addHeader ("Content-type" , "application/x-www-form-urlencoded" );
463+
464+ ByteArrayEntity dataToSend ;
465+ if (!getIsMultipart () && getVariablesToSend ().size () > 0 )
466+ dataToSend = new ByteArrayEntity (CommonUtil .hashtable2query (getVariablesToSend ()).getBytes ());
467+ else
468+ dataToSend = new ByteArrayEntity (getData ());
469+ httpPost .setEntity (dataToSend );
470+
471+ response = httpClient .execute (httpPost , httpClientContext );
472+
473+ } else if (method .equalsIgnoreCase ("PUT" )) {
474+ HttpPut httpPut = new HttpPut (url .trim ());
475+ httpPut .setConfig (reqConfig );
476+ Set <String > keys = getheadersToSend ().keySet ();
477+ for (String header : keys ) {
478+ httpPut .addHeader (header , getheadersToSend ().get (header ));
479+ }
480480
481- httpPut .setEntity (new ByteArrayEntity (getData ()));
481+ httpPut .setEntity (new ByteArrayEntity (getData ()));
482482
483- response = httpClient .execute (httpPut , httpClientContext );
483+ response = httpClient .execute (httpPut , httpClientContext );
484484
485- } else if (method .equalsIgnoreCase ("DELETE" )) {
486- HttpDeleteWithBody httpDelete = new HttpDeleteWithBody (url .trim ());
487- httpDelete .setConfig (reqConfig );
488- Set <String > keys = getheadersToSend ().keySet ();
489- for (String header : keys ) {
490- httpDelete .addHeader (header ,getheadersToSend ().get (header ));
491- }
485+ } else if (method .equalsIgnoreCase ("DELETE" )) {
486+ HttpDeleteWithBody httpDelete = new HttpDeleteWithBody (url .trim ());
487+ httpDelete .setConfig (reqConfig );
488+ Set <String > keys = getheadersToSend ().keySet ();
489+ for (String header : keys ) {
490+ httpDelete .addHeader (header , getheadersToSend ().get (header ));
491+ }
492492
493- if (getVariablesToSend ().size () > 0 || getContentToSend ().size () > 0 )
494- httpDelete .setEntity (new ByteArrayEntity (getData ()));
493+ if (getVariablesToSend ().size () > 0 || getContentToSend ().size () > 0 )
494+ httpDelete .setEntity (new ByteArrayEntity (getData ()));
495495
496- response = httpClient .execute (httpDelete , httpClientContext );
496+ response = httpClient .execute (httpDelete , httpClientContext );
497497
498- } else if (method .equalsIgnoreCase ("HEAD" )) {
499- HttpHeadWithBody httpHead = new HttpHeadWithBody (url .trim ());
500- httpHead .setConfig (reqConfig );
501- Set <String > keys = getheadersToSend ().keySet ();
502- for (String header : keys ) {
503- httpHead .addHeader (header ,getheadersToSend ().get (header ));
504- }
498+ } else if (method .equalsIgnoreCase ("HEAD" )) {
499+ HttpHeadWithBody httpHead = new HttpHeadWithBody (url .trim ());
500+ httpHead .setConfig (reqConfig );
501+ Set <String > keys = getheadersToSend ().keySet ();
502+ for (String header : keys ) {
503+ httpHead .addHeader (header , getheadersToSend ().get (header ));
504+ }
505505
506- httpHead .setEntity (new ByteArrayEntity (getData ()));
506+ httpHead .setEntity (new ByteArrayEntity (getData ()));
507507
508- response = httpClient .execute (httpHead , httpClientContext );
508+ response = httpClient .execute (httpHead , httpClientContext );
509509
510- } else if (method .equalsIgnoreCase ("CONNECT" )) {
511- HttpConnectMethod httpConnect = new HttpConnectMethod (url .trim ());
512- httpConnect .setConfig (reqConfig );
513- Set <String > keys = getheadersToSend ().keySet ();
514- for (String header : keys ) {
515- httpConnect .addHeader (header ,getheadersToSend ().get (header ));
516- }
517- response = httpClient .execute (httpConnect , httpClientContext );
518-
519- } else if (method .equalsIgnoreCase ("OPTIONS" )) {
520- HttpOptionsWithBody httpOptions = new HttpOptionsWithBody (url .trim ());
521- httpOptions .setConfig (reqConfig );
522- Set <String > keys = getheadersToSend ().keySet ();
523- for (String header : keys ) {
524- httpOptions .addHeader (header ,getheadersToSend ().get (header ));
525- }
510+ } else if (method .equalsIgnoreCase ("CONNECT" )) {
511+ HttpConnectMethod httpConnect = new HttpConnectMethod (url .trim ());
512+ httpConnect .setConfig (reqConfig );
513+ Set <String > keys = getheadersToSend ().keySet ();
514+ for (String header : keys ) {
515+ httpConnect .addHeader (header , getheadersToSend ().get (header ));
516+ }
517+ response = httpClient .execute (httpConnect , httpClientContext );
518+
519+ } else if (method .equalsIgnoreCase ("OPTIONS" )) {
520+ HttpOptionsWithBody httpOptions = new HttpOptionsWithBody (url .trim ());
521+ httpOptions .setConfig (reqConfig );
522+ Set <String > keys = getheadersToSend ().keySet ();
523+ for (String header : keys ) {
524+ httpOptions .addHeader (header , getheadersToSend ().get (header ));
525+ }
526526
527- httpOptions .setEntity (new ByteArrayEntity (getData ()));
527+ httpOptions .setEntity (new ByteArrayEntity (getData ()));
528528
529- response = httpClient .execute (httpOptions , httpClientContext );
529+ response = httpClient .execute (httpOptions , httpClientContext );
530530
531- } else if (method .equalsIgnoreCase ("TRACE" )) { // No lleva payload
532- HttpTrace httpTrace = new HttpTrace (url .trim ());
533- httpTrace .setConfig (reqConfig );
534- Set <String > keys = getheadersToSend ().keySet ();
535- for (String header : keys ) {
536- httpTrace .addHeader (header ,getheadersToSend ().get (header ));
537- }
538- response = httpClient .execute (httpTrace , httpClientContext );
539-
540- } else if (method .equalsIgnoreCase ("PATCH" )) {
541- HttpPatch httpPatch = new HttpPatch (url .trim ());
542- httpPatch .setConfig (reqConfig );
543- Set <String > keys = getheadersToSend ().keySet ();
544- for (String header : keys ) {
545- httpPatch .addHeader (header ,getheadersToSend ().get (header ));
531+ } else if (method .equalsIgnoreCase ("TRACE" )) { // No lleva payload
532+ HttpTrace httpTrace = new HttpTrace (url .trim ());
533+ httpTrace .setConfig (reqConfig );
534+ Set <String > keys = getheadersToSend ().keySet ();
535+ for (String header : keys ) {
536+ httpTrace .addHeader (header , getheadersToSend ().get (header ));
537+ }
538+ response = httpClient .execute (httpTrace , httpClientContext );
539+
540+ } else if (method .equalsIgnoreCase ("PATCH" )) {
541+ HttpPatch httpPatch = new HttpPatch (url .trim ());
542+ httpPatch .setConfig (reqConfig );
543+ Set <String > keys = getheadersToSend ().keySet ();
544+ for (String header : keys ) {
545+ httpPatch .addHeader (header , getheadersToSend ().get (header ));
546+ }
547+ ByteArrayEntity dataToSend = new ByteArrayEntity (getData ());
548+ httpPatch .setEntity (dataToSend );
549+ response = httpClient .execute (httpPatch , httpClientContext );
546550 }
547- ByteArrayEntity dataToSend = new ByteArrayEntity (getData ());
548- httpPatch .setEntity (dataToSend );
549- response = httpClient .execute (httpPatch , httpClientContext );
550551 }
551-
552552 statusCode = response .getStatusLine ().getStatusCode ();
553553 reasonLine = response .getStatusLine ().getReasonPhrase ();
554554
0 commit comments