Kyrexar

[IAP] AplicacionHTTP

Oct 16th, 2019 (edited)
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.65 KB | None | 0 0
  1. package aplicacionHTTP;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.net.HttpURLConnection;
  7. import java.net.URL;
  8. import java.util.concurrent.TimeoutException;
  9.  
  10. import org.json.JSONArray;
  11. import org.json.JSONException;
  12. import org.json.JSONObject;
  13.  
  14. import com.rabbitmq.client.BuiltinExchangeType;
  15. import com.rabbitmq.client.Channel;
  16. import com.rabbitmq.client.Connection;
  17. import com.rabbitmq.client.ConnectionFactory;
  18.  
  19. public class AplicacionHTTP {
  20.     private final static String NOMBRE_EXCHANGE = "fallas";
  21.  
  22.     public static void main(String[] args) throws IOException, JSONException, TimeoutException {
  23.         // GET
  24.         String url = "http://mapas.valencia.es/lanzadera/puntoInteres/fallasvalencia?radio=4000&lang=es&lat=39465212&lon=-374521&filtros=2";
  25.         String userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";
  26.         String authorization = "Basic aW5mb3JtYXR1cHY6dHlBMGJxVWU=";
  27.  
  28.         // Creamos URL
  29.         URL objUrl = new URL(url);
  30.  
  31.         // Creamos conexión HTTP
  32.         HttpURLConnection con = (HttpURLConnection) objUrl.openConnection();
  33.  
  34.         // Definimos operación HTTP
  35.         con.setRequestMethod("GET");
  36.  
  37.         // Añadimos encabezados
  38.         con.setRequestProperty("authorization", authorization);
  39.         con.setRequestProperty("user-agent", userAgent);
  40.  
  41.         // Procesamos la respuesta
  42.         BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
  43.         String inputLine;
  44.         StringBuffer response = new StringBuffer();
  45.         while ((inputLine = in.readLine()) != null) {
  46.             response.append(inputLine);
  47.         }
  48.         in.close();
  49.        
  50.         String fallas = response.toString();
  51.        
  52.         JSONArray coleccion=new JSONArray(fallas);
  53.  
  54.         //System.out.println(fallas);
  55.  
  56.         // Conectarse con RabbitMQ
  57.         ConnectionFactory factory = new ConnectionFactory();
  58.         factory.setHost("localhost");
  59.         Connection connection = factory.newConnection();
  60.  
  61.         // Crear un canal de comunicación
  62.         Channel channel = connection.createChannel();
  63.  
  64.         // Declaramos un Exchange de tipo Topic
  65.         channel.exchangeDeclare(NOMBRE_EXCHANGE, BuiltinExchangeType.TOPIC);
  66.  
  67.         // Enviar mensajes con tema de enrutamiento
  68.         for( int i=0 ; i<coleccion.length() ; i++ ) {
  69.             JSONObject falla=coleccion.getJSONObject(i);
  70.             int distancia = falla.getInt("distancia");
  71.             if( distancia>1000 ) {
  72.                 channel.basicPublish(NOMBRE_EXCHANGE, "lejos", null, falla.toString().getBytes());
  73.             }else {
  74.                 channel.basicPublish(NOMBRE_EXCHANGE, "cerca", null, falla.toString().getBytes());
  75.             }
  76.         }
  77.  
  78.         // Cerrar canal y conexión, si no se quiere enviar más
  79.         channel.close();
  80.         connection.close();
  81.     }
  82. }
Add Comment
Please, Sign In to add comment