Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. public DatosConsultaAgente GetConsultaMatricula(Agente agente, String matricula) throws Exception {
  2. URL url = new URL(URLservice);
  3.  
  4. HttpURLConnection con = (HttpURLConnection) url.openConnection();
  5. con.setDoOutput(true);
  6. con.setDoInput(true);
  7. con.setRequestProperty("Content-Type", "application/json");
  8. con.setRequestProperty("Accept", "application/json");
  9. con.setRequestMethod("POST");
  10.  
  11.  
  12.  
  13. DatosConsultaAgente sendDatos = new DatosConsultaAgente();
  14. sendDatos.setAgente(agente);
  15. sendDatos.setMatricula(matricula);
  16.  
  17.  
  18. //Creo una instancia de Gson para parsear los objetos
  19. Gson gson = new Gson();
  20.  
  21. byte[] outputBytes = PBE.getInstance().Encripta(gson.toJson(sendDatos)).getBytes("UTF-8");
  22. OutputStream os = con.getOutputStream();
  23. os.write(outputBytes);
  24. os.flush();
  25.  
  26. if (con.getResponseCode() != HttpURLConnection.HTTP_OK &&
  27. con.getResponseCode() != HttpURLConnection.HTTP_ACCEPTED &&
  28. con.getResponseCode() != HttpURLConnection.HTTP_CREATED){
  29. throw new RuntimeException("Failed : HTTP error code : "
  30. + con.getResponseCode());
  31. }
  32.  
  33. BufferedReader br = new BufferedReader(new InputStreamReader(
  34. (con.getInputStream())));
  35.  
  36. String output;
  37. System.out.println("Output from Server .... \n");
  38. StringBuilder responseString = new StringBuilder();
  39. while ((output = br.readLine()) != null) {
  40. System.out.println(output);
  41. responseString.append(output);
  42. }
  43.  
  44. con.disconnect();
  45. DatosConsultaAgente datosRespuesta = gson.fromJson(responseString.toString(), DatosConsultaAgente.class);
  46.  
  47.  
  48. return datosRespuesta;
  49.  
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement