iscomar001
12/6/2018 - 12:05 AM

Http reques

Ejecuta un requues http

private static String execute(String RequestDateTime,String authToken, String Data) throws ModelException{
		
		StringBuilder responseStrBuilder = new StringBuilder();
	        StringBuilder body = new StringBuilder();
	        
	        CloseableHttpClient httpClient = null;
	        HttpResponse response = null;
	        InputStream in = null;
	        BufferedReader streamReader = null;
	        
	    	try {
	    		
	            int TIMEOUT_MILLIS = 180000;
	    		
	            httpClient = HttpClientBuilder.create().build();
	    		
	            HttpPost post = new HttpPost(URL_CALL_HISTORY);
	            post.addHeader("Content-type", "text/xml");

	            RequestConfig requestConfig = RequestConfig.custom()
	                              .setSocketTimeout(TIMEOUT_MILLIS)
	                              .setConnectTimeout(TIMEOUT_MILLIS)
	                              .setConnectionRequestTimeout(TIMEOUT_MILLIS)
	                              .build();

	            post.setConfig(requestConfig);

	            body.append("AuthUsername = ").append(userName).append("\n");
	            body.append("AuthToken = ").append(authToken).append("\n");
	            body.append("RequestDateTime = ").append(RequestDateTime).append("\n");
	            body.append("Data = ").append(Data);
	            
	            StringEntity postingString = new StringEntity(body.toString(), Charset.forName("UTF-8"));
	            post.setEntity(postingString);
	    		
	            response = httpClient.execute(post);

	            if (response != null) {
	                in = response.getEntity().getContent(); //Get the data in the entity
	                
	                streamReader = new BufferedReader(new InputStreamReader(in, "UTF-8"));

	                String inputStr;
	                while ((inputStr = streamReader.readLine()) != null){
	                    responseStrBuilder.append(inputStr);
	                }  
	            }
	            
	            //logger.debug("RESPONSE: " + responseStrBuilder.toString());
	    	
	        } catch (Exception e) {
	            logger.error("ERROR:execute, ERROR: " + e.getMessage());
	            throw new ModelException("Error EXECUTE: " + e.getMessage());
	    	}finally {
	    		try {
					streamReader.close();
					in.close();
					httpClient.close();
				} catch (IOException e) {
					logger.error("ERROR:execute finally, ERROR: " + e.getMessage());
				}
			}
	    	
	    	return responseStrBuilder.toString();
    }