Skip to content

Instantly share code, notes, and snippets.

@betray32
Last active January 27, 2020 19:40
Show Gist options
  • Save betray32/663ad35515bc115c9d9f0aeac765bd34 to your computer and use it in GitHub Desktop.
Save betray32/663ad35515bc115c9d9f0aeac765bd34 to your computer and use it in GitHub Desktop.
Cliente REST application/x-www-form-urlencoded
/**
* Invocar Consulta Abono Periodico
*/
public ResponseConsultaAbonoPeriodico consultaAbonoPeriodico(DtoConsultaAbonoPeriodico request, DtoRequestDatosContextoHttp contexto) {
log.info(MENSAJE_INICIO_DAO + "[consultaAbonoPeriodico]");
log.info("Endpoint: " + Constantes.ENDPOINT_CCB_CONSULTA_ABONO);
ResponseConsultaAbonoPeriodico res = null;
try {
URL url = new URL(Constantes.ENDPOINT_CCB_CONSULTA_ABONO);
Map<String, Object> params = new LinkedHashMap<>();
params.put("rut", request.getRutCliente());
params.put("codigo_aplicacion", contexto.getCodigoAplicacion());
params.put("cliente_id", contexto.getClientId());
params.put("request_id", contexto.getRequestID());
params.put("empresa_aplicacion", contexto.getEmpresaAplicacion());
params.put("ip_cliente", contexto.getIpCliente());
params.put("codigo_canal", contexto.getCodigoCanal());
StringBuilder postData = new StringBuilder();
log.info("Parametros de Entrada");
for (Map.Entry<String, Object> param : params.entrySet()) {
if (postData.length() != 0)
postData.append('&');
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
log.info("Key = ["+param.getKey()+"]" + " , Value = ["+param.getValue()+"]");
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
// Headers y metadata
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", contexto.getToken());
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);
/****************************************
* Iterar Salida
*/
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
StringBuilder sb = new StringBuilder();
for (int c; (c = in.read()) >= 0;) {
sb.append((char) c);
}
String response = sb.toString();
log.info("Raw Output: " + response);
/****************************************/
Gson g = new Gson();
res = g.fromJson(response, ResponseConsultaAbonoPeriodico.class);
log.info("Servicio Invocado OK");
} catch (Exception e) {
log.error("Error en invocar servicio CCB , Detalle > ", e);
}
log.info(FINALIZA_REST + "[consultaAbonoPeriodico]");
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment