@@ -112,7 +112,6 @@ public void setTimeout(int timeout)
112
112
private String reasonLine = "" ;
113
113
private HttpClientBuilder httpClientBuilder ;
114
114
private HttpClientContext httpClientContext = null ;
115
- private CloseableHttpClient httpClient = null ;
116
115
private CloseableHttpResponse response = null ;
117
116
private CredentialsProvider credentialsProvider = null ;
118
117
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) {
333
332
this .httpClientBuilder .setDefaultCookieStore (cookiesToSend ); // Cookies Seteo CookieStore
334
333
}
335
334
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
+
336
345
if (getProxyInfoChanged () && !getProxyServerHost ().isEmpty () && getProxyServerPort () != 0 ) {
337
346
HttpHost proxy = new HttpHost (getProxyServerHost (), getProxyServerPort ());
338
347
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 );
350
349
}
351
350
351
+ this .reqConfig = requestConfigBuilder .build ();
352
+
352
353
if (getHostChanged () || getAuthorizationChanged ()) { // Si el host cambio o si se agrego alguna credencial
353
354
this .credentialsProvider = new BasicCredentialsProvider ();
354
355
@@ -434,121 +435,120 @@ public void execute(String method, String url) {
434
435
else
435
436
url = url .startsWith ("http://" ) ? url : "http://" + getHost () + ":" + (getPort () == -1 ? "80" :getPort ()) + url ;
436
437
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
+ }
446
446
447
- httpget .setEntity (new ByteArrayEntity (getData ()));
447
+ httpget .setEntity (new ByteArrayEntity (getData ()));
448
448
449
- response = httpClient .execute (httpget , httpClientContext );
449
+ response = httpClient .execute (httpget , httpClientContext );
450
450
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
+ }
480
480
481
- httpPut .setEntity (new ByteArrayEntity (getData ()));
481
+ httpPut .setEntity (new ByteArrayEntity (getData ()));
482
482
483
- response = httpClient .execute (httpPut , httpClientContext );
483
+ response = httpClient .execute (httpPut , httpClientContext );
484
484
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
+ }
492
492
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 ()));
495
495
496
- response = httpClient .execute (httpDelete , httpClientContext );
496
+ response = httpClient .execute (httpDelete , httpClientContext );
497
497
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
+ }
505
505
506
- httpHead .setEntity (new ByteArrayEntity (getData ()));
506
+ httpHead .setEntity (new ByteArrayEntity (getData ()));
507
507
508
- response = httpClient .execute (httpHead , httpClientContext );
508
+ response = httpClient .execute (httpHead , httpClientContext );
509
509
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
+ }
526
526
527
- httpOptions .setEntity (new ByteArrayEntity (getData ()));
527
+ httpOptions .setEntity (new ByteArrayEntity (getData ()));
528
528
529
- response = httpClient .execute (httpOptions , httpClientContext );
529
+ response = httpClient .execute (httpOptions , httpClientContext );
530
530
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 );
546
550
}
547
- ByteArrayEntity dataToSend = new ByteArrayEntity (getData ());
548
- httpPatch .setEntity (dataToSend );
549
- response = httpClient .execute (httpPatch , httpClientContext );
550
551
}
551
-
552
552
statusCode = response .getStatusLine ().getStatusCode ();
553
553
reasonLine = response .getStatusLine ().getReasonPhrase ();
554
554
0 commit comments