isc_415_practica_1.ISC_415_Practica_1.java Source code

Java tutorial

Introduction

Here is the source code for isc_415_practica_1.ISC_415_Practica_1.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package isc_415_practica_1;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.TextNode;

import org.apache.http.Consts;
import static org.apache.http.HttpHeaders.USER_AGENT;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

/**
 *
 * @author Stanley
 */
public class ISC_415_Practica_1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String urlString;
        Scanner input = new Scanner(System.in);
        Document doc;

        try {
            urlString = input.next();
            if (urlString.equals("servlet")) {
                urlString = "http://localhost:8084/ISC_415_Practica1_Servlet/client";
            }
            urlString = urlString.contains("http://") || urlString.contains("https://") ? urlString
                    : "http://" + urlString;
            doc = Jsoup.connect(urlString).get();
        } catch (Exception ex) {
            System.out.println("El URL ingresado no es valido.");
            return;
        }

        ArrayList<NameValuePair> formInputParams;
        formInputParams = new ArrayList<>();
        String[] plainTextDoc = new TextNode(doc.html(), "").getWholeText().split("\n");
        System.out.println(String.format("Nmero de lineas del documento: %d", plainTextDoc.length));
        System.out.println(String.format("Nmero de p tags: %d", doc.select("p").size()));
        System.out.println(String.format("Nmero de img tags: %d", doc.select("img").size()));
        System.out.println(String.format("Nmero de form tags: %d", doc.select("form").size()));

        Integer index = 1;

        ArrayList<NameValuePair> urlParameters = new ArrayList<>();
        for (Element e : doc.select("form")) {
            System.out.println(String.format("Form %d: Nmero de Input tags %d", index, e.select("input").size()));
            System.out.println(e.select("input"));

            for (Element formInput : e.select("input")) {
                if (formInput.attr("id") != null && formInput.attr("id") != "") {
                    urlParameters.add(new BasicNameValuePair(formInput.attr("id"), "PRACTICA1"));
                } else if (formInput.attr("name") != null && formInput.attr("name") != "") {
                    urlParameters.add(new BasicNameValuePair(formInput.attr("name"), "PRACTICA1"));
                }
            }

            index++;
        }

        if (!urlParameters.isEmpty()) {
            try {
                CloseableHttpClient httpclient = HttpClients.createDefault();
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(urlParameters, Consts.UTF_8);
                HttpPost httpPost = new HttpPost(urlString);
                httpPost.setHeader("User-Agent", USER_AGENT);
                httpPost.setEntity(entity);
                HttpResponse response = httpclient.execute(httpPost);
                System.out.println(response.getStatusLine());
            } catch (IOException ex) {
                Logger.getLogger(ISC_415_Practica_1.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

    }

}