Mi primer clase en Java , se llama DH Tools y tiene las siguientes opciones :

[+] Realizar una peticion GET y guardar el contenido
[+] Realizar una peticion POST y guardar el contenido
[+] Crear o escribir archivos
[+] Leer archivos
[+] Ejecutar comandos y leer su respuesta
[+] HTTP FingerPrinting
[+] Leer el codigo de respuesta de una URL
[+] Borrar repetidos en un ArrayList
[+] Cortar las URL en un ArrayList a partir del query
[+] Split casero xD
[+] Descargar archivos
[+] Capturar el archivo de una URL
[+] URI Split
[+] MD5 Encode
[+] MD5 File
[+] Get IP

El codigo de la clase :
// Class : DH Tools
// Version : 0.2
// (C) Doddy Hackman 2015
// Functions : 
//
//public String toma(String link) 
//public String tomar(String pagina, String data) 
//public void savefile(String ruta, String texto) 
//public String read_file(String ruta) 
//public String console(String command) 
//public String httpfinger(String target) 
//public Integer response_code(String page) 
//public ArrayList repes(ArrayList array) 
//public ArrayList cortar(ArrayList array) 
//public String regex(String code, String deaca, String hastaaca) 
//public Boolean download(String url, File savefile) 
//public String extract_file_by_url(String url) 
//public String uri_split(String link, String opcion) 
//public String md5_encode(String text) 
//public String md5_file(String file) 
//public String get_ip(String hostname) 
//
package dhtools;

import java.io.*;
import java.net.*;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.security.*;

public class DH_Tools {

    public String toma(String link) {
        String re;
        StringBuffer conte = new StringBuffer(40);
        try {
            URL url = new URL(link);
            URLConnection nave = url.openConnection();
            nave.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0");
            BufferedReader leyendo = new BufferedReader(
                    new InputStreamReader(nave.getInputStream()));
            while ((re = leyendo.readLine()) != null) {
                conte.append(re);
            }
            leyendo.close();
        } catch (Exception e) {
            //
        }
        return conte.toString();
    }

    public String tomar(String pagina, String data) {
        // Credits : Function based in http://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/
        String respuesta = "";

        try {
            URL url_now = new URL(pagina);
            HttpURLConnection nave = (HttpURLConnection) url_now.openConnection();

            nave.setRequestMethod("POST");
            nave.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0");

            nave.setDoOutput(true);
            DataOutputStream send = new DataOutputStream(nave.getOutputStream());
            send.writeBytes(data);
            send.flush();
            send.close();

            BufferedReader leyendo = new BufferedReader(new InputStreamReader(nave.getInputStream()));
            StringBuffer code = new StringBuffer();
            String linea;

            while ((linea = leyendo.readLine()) != null) {
                code.append(linea);
            }
            leyendo.close();
            respuesta = code.toString();
        } catch (Exception e) {
            //
        }
        return respuesta;
    }

    public void savefile(String ruta, String texto) {

        FileWriter escribir = null;
        File archivo = null;

        try {

            archivo = new File(ruta);

            if (!archivo.exists()) {
                archivo.createNewFile();
            }

            escribir = new FileWriter(archivo, true);
            escribir.write(texto);
            escribir.flush();
            escribir.close();

        } catch (Exception e) {
            //
        }
    }

    public String read_file(String ruta) {
        String contenido = null;
        try {
            Scanner leyendo = new Scanner(new FileReader(ruta));
            contenido = leyendo.next();
        } catch (Exception e) {
            //
        }
        return contenido;
    }

    public String console(String command) {
        String contenido = null;
        try {
            Process proceso = Runtime.getRuntime().exec("cmd /c " + command);
            proceso.waitFor();
            BufferedReader leyendo = new BufferedReader(
                    new InputStreamReader(proceso.getInputStream()));
            String linea;
            StringBuffer code = new StringBuffer();
            while ((linea = leyendo.readLine()) != null) {
                code.append(linea);
            }
            contenido = code.toString();
        } catch (Exception e) {
            //
        }
        return contenido;
    }

    public String httpfinger(String target) {

        String resultado = "";

        //http://www.mkyong.com/java/how-to-get-http-response-header-in-java/
        try {

            URL page = new URL(target);
            URLConnection nave = page.openConnection();

            String server = nave.getHeaderField("Server");
            String etag = nave.getHeaderField("ETag");
            String content_length = nave.getHeaderField("Content-Length");
            String expires = nave.getHeaderField("Expires");
            String last_modified = nave.getHeaderField("Last-Modified");
            String connection = nave.getHeaderField("Connection");
            String powered = nave.getHeaderField("X-Powered-By");
            String pragma = nave.getHeaderField("Pragma");
            String cache_control = nave.getHeaderField("Cache-Control");
            String date = nave.getHeaderField("Date");
            String vary = nave.getHeaderField("Vary");
            String content_type = nave.getHeaderField("Content-Type");
            String accept_ranges = nave.getHeaderField("Accept-Ranges");

            if (server != null) {
                resultado += "[+] Server : " + server + "\n";
            }
            if (etag != null) {
                resultado += "[+] E-tag : " + etag + "\n";
            }
            if (content_length != null) {
                resultado += "[+] Content-Length : " + content_length + "\n";
            }

            if (expires != null) {
                resultado += "[+] Expires : " + expires + "\n";
            }

            if (last_modified != null) {
                resultado += "[+] Last Modified : " + last_modified + "\n";
            }

            if (connection != null) {
                resultado += "[+] Connection : " + connection + "\n";
            }

            if (powered != null) {
                resultado += "[+] Powered : " + powered + "\n";
            }

            if (pragma != null) {
                resultado += "[+] Pragma : " + pragma + "\n";
            }

            if (cache_control != null) {
                resultado += "[+] Cache control : " + cache_control + "\n";
            }

            if (date != null) {
                resultado += "[+] Date : " + date + "\n";
            }
            if (vary != null) {
                resultado += "[+] Vary : " + vary + "\n";
            }
            if (content_type != null) {
                resultado += "[+] Content-Type : " + content_type + "\n";
            }
            if (accept_ranges != null) {
                resultado += "[+] Accept Ranges : " + accept_ranges + "\n";
            }

        } catch (Exception e) {
            //
        }

        return resultado;

    }

    public Integer response_code(String page) {
        Integer response = 0;
        try {
            URL url = new URL(page);
            URLConnection nave1 = url.openConnection();
            HttpURLConnection nave2 = (HttpURLConnection) nave1;
            nave2.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0");
            response = nave2.getResponseCode();
        } catch (Exception e) {
            response = 404;
        }
        return response;
    }

    public ArrayList repes(ArrayList array) {
        Object[] listando = array.toArray();
        for (Object item : listando) {
            if (array.indexOf(item) != array.lastIndexOf(item)) {
                array.remove(array.lastIndexOf(item));
            }
        }
        return array;
    }

    public ArrayList cortar(ArrayList array) {
        ArrayList array2 = new ArrayList();
        for (int i = 0; i < array.size(); i++) {
            String code = (String) array.get(i);
            Pattern regex1 = null;
            Matcher regex2 = null;
            regex1 = Pattern.compile("(.*?)=(.*?)");
            regex2 = regex1.matcher(code);
            if (regex2.find()) {
                array2.add(regex2.group(1) + "=");
            }
        }
        return array2;
    }

    public String regex(String code, String deaca, String hastaaca) {
        String resultado = "";
        Pattern regex1 = null;
        Matcher regex2 = null;
        regex1 = Pattern.compile(deaca + "(.*?)" + hastaaca);
        regex2 = regex1.matcher(code);
        if (regex2.find()) {
            resultado = regex2.group(1);
        }
        return resultado;
    }

    public Boolean download(String url, File savefile) {
        // Credits : Based on http://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java
        // Thanks to Brian Risk
        try {
            URL download_page = new URL(url);
            ReadableByteChannel down1 = Channels.newChannel(download_page.openStream());
            FileOutputStream down2 = new FileOutputStream(savefile);
            down2.getChannel().transferFrom(down1, 0, Long.MAX_VALUE);
            down1.close();
            down2.close();
            return true;
        } catch (IOException e) {
            return false;
        }
    }

    public String extract_file_by_url(String url) {
        return url.substring(url.lastIndexOf('/') + 1);
    }

    public String uri_split(String link, String opcion) {
        String resultado = "";
        try {
            URL url = new URL(link);
            if (opcion == "protocol") {
                resultado = url.getProtocol();
            } else if (opcion == "authority") {
                resultado = url.getAuthority();
            } else if (opcion == "host") {
                resultado = url.getHost();
            } else if (opcion == "port") {
                resultado = String.valueOf(url.getPort());
            } else if (opcion == "path") {
                resultado = url.getPath();
            } else if (opcion == "query") {
                resultado = url.getQuery();
            } else if (opcion == "filename") {
                resultado = url.getFile();
            } else if (opcion == "ref") {
                resultado = url.getRef();
            } else {
                resultado = "Error";
            }

        } catch (Exception e) {
            // 
        }
        return resultado;
    }

    public String md5_encode(String text) {
        // Credits : Based on http://www.avajava.com/tutorials/lessons/how-do-i-generate-an-md5-digest-for-a-string.html
        StringBuffer string_now = null;
        try {
            MessageDigest generate = MessageDigest.getInstance("MD5");
            generate.update(text.getBytes());
            byte[] result = generate.digest();
            string_now = new StringBuffer();
            for (byte line : result) {
                string_now.append(String.format("%02x", line & 0xff));
            }
        } catch (Exception e) {
            //
        }
        return string_now.toString();
    }

    public String md5_file(String file) {
        //Credits : Based on http://stackoverflow.com/questions/304268/getting-a-files-md5-checksum-in-java
        // Thanks to 
        String resultado = "";
        try {
            MessageDigest convert = MessageDigest.getInstance("MD5");
            FileInputStream file_now = new FileInputStream(file);

            byte[] bytes_now = new byte[1024];

            int now_now = 0;
            while ((now_now = file_now.read(bytes_now)) != -1) {
                convert.update(bytes_now, 0, now_now);
            };
            byte[] converting = convert.digest();
            StringBuffer result = new StringBuffer();
            for (int i = 0; i < converting.length; i++) {
                result.append(Integer.toString((converting[i] & 0xff) + 0x100, 16).substring(1));
            }
            resultado = result.toString();
        } catch (Exception e) {
            //
        }
        return resultado;
    }

    public String get_ip(String hostname) {
        String resultado = "";
        try {
            InetAddress getting_ip = InetAddress.getByName(hostname);
            resultado = getting_ip.getHostAddress();
        } catch (Exception e) {
            //
        }
        return resultado;
    }
}

// The End ?
Ejemplos de uso :
package dhtools;

import java.util.ArrayList;
import java.util.Collections;

public class Main {

    public static void main(String[] args) {
        DH_Tools tools = new DH_Tools();
        //String codigo = tools.toma("http://localhost/");
        //String codigo = tools.tomar("http://localhost/login.php", "usuario=test&password=dsdsads&control=Login");
        //tools.savefile("c:/xampp/texto.txt","texto");
        //String codigo = tools.read_file("c:/xampp/texto.txt");
        //String codigo = tools.console("ver");
        //String codigo = tools.httpfinger("http://www.petardas.com");
        /*
         ArrayList array = new ArrayList();
         Collections.addAll(array, "http://localhost/sql.php?id=dsaadsds", "b", "http://localhost/sql.php?id=dsaadsds", "c");
         ArrayList array2 = tools.repes(tools.cortar(array));
         for (int i = 0; i < array2.size(); i++) {
         System.out.println(array2.get(i));
         }
         */
        //System.out.println(tools.regex("1sadasdsa2","1","2"));
        //System.out.println(tools.response_code("http://www.petardas.com/"));
        /*
         File savefile = new File("c:/xampp/mierda.avi");
         if(tools.download("http://localhost/test.avi",savefile)) {
         System.out.println("yeah");
         }
         */

        //System.out.println(tools.extract_file_by_url("http://localhost/dsaads/dsadsads/index.php"));
        //System.out.println(tools.uri_split("http://localhost/index.php?id=dadsdsa","query"));
        //System.out.println(tools.md5_encode("123"));
        //System.out.println(tools.md5_file("c:\\xampp\\texto.txt"));
        //System.out.println(tools.get_ip("www.petardas.com"));
    }

}
Eso seria todo.
Responder

Volver a “Fuentes”