Java website software integration

Asked

Viewed 107 times

1

My question is this: My team and I are developing a software that analyzes a table with specific data, but to generate this table we use a website where you enter information and this website analyzes and generates all the data on this information in the form of an html table. Most of us have basic and intermediate knowledge of java, so I would like to know if there is a way to make a program that will receive the input, consult this website, enter the past information, collect the result in html and return only this result already ready.

Edit: Here is the part of my code that makes the connection, the site receives a genetic sequence, processes and then returns a table in html. What I want is for my system to connect, send the sequence and capture the answer.

package wangConn;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

public class WangConnection {
public static void main(String[] args) {
    String param1 = "ATGCCCCCGACGCCCCCGGGGGGTGA", param2="Submit";
    URL url;
    HttpURLConnection connection = null;
    BufferedReader in = null;
    DataOutputStream out = null;
    try {
        // Mounting content to be sent
        String data = "seqfield=" + URLEncoder.encode(param1, "UTF-8");
        data += "Submit=" + URLEncoder.encode(param2, "UTF-8");

        url = new URL("http://wangcomputing.com/assp/");
        // Make connection with the specified URL
        connection = (HttpURLConnection)url.openConnection();
        // Enable writing on URLConnection
        connection.setDoOutput(true);
        // Connect using the POST method
        connection.setRequestMethod("POST");
        // Define the requisition type
        connection.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");

        // Send data
        out = new DataOutputStream(
                connection.getOutputStream());
        out.writeBytes(data);
        out.flush();
        out.close();

        // Getting return from the connection
        String line;
        in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        while((line = in.readLine()) != null) {
            System.out.println(line);
        }
    } 
    catch (MalformedURLException ex) {
        System.out.println("Bad format URL: " + ex.getMessage());
    }
    catch (IOException ex) {
        System.out.println("Unable to connect: " + ex.getMessage());
    }
    finally {
        if(out != null) {
            try {
                out.close();
            } 
            catch (IOException ex) {
                System.out.println("Unable to close input stream: " + ex.getMessage());
            }
        }
        if(in != null) {
            try {
                in.close();
            } 
            catch (IOException ex) {
                System.out.println("Unable to close output stream: " + ex.getMessage());
            }
        }
        connection.disconnect();
    }
  }
}

My problem now is that the website I am trying to submit the form on the website has failed. The type of input used:

  • Welcome to Stack Overflow! Your question is somewhat broad @Lubu, and this type of question does not fit very well in the proposal of the site. Try to divide your problem into parts, develop some code and if you find specific questions post here. Start by doing a [tour] and read [Ask]. Hug.

  • your question is not broad, it is obscure and not technical. you want to fetch data from a page, ok you need to inform how this access is done, get or post and if there is authentication. then your doubt and how to take html analyze and extract some information.

  • Basically what I look for is a way for my system to access a web page, insert the content that this page asks to generate a result, capture this result and bring back to my system. That is, I want my system to use a web page to generate a result that will be used in another part of the system later. I don’t want code ready or anything.

1 answer

1


You need to enter the URL and capture the information within the element you want. Here’s how to access a java URL:

public static void main(String[] args) {
    URL url;
    InputStream is = null;
    BufferedReader br;
    String line;

    try {
        url = new URL("http://stackoverflow.com/");
        is = url.openStream();  // throws an IOException
        br = new BufferedReader(new InputStreamReader(is));

        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
    } catch (MalformedURLException mue) {
         mue.printStackTrace();
    } catch (IOException ioe) {
         ioe.printStackTrace();
    } finally {
        try {
            if (is != null) is.close();
        } catch (IOException ioe) {
            // nothing to see here
        }
    }
}

When you have HTML, you may want to filter it. Look at this link showing an alternative. JSOUP

String html = "<p>An <a href='http://example.com/'><b>example</b></a> link.</p>";
Document doc = Jsoup.parse(html);
Element link = doc.select("a").first();

String text = doc.body().text(); // "An example link"
String linkHref = link.attr("href"); // "http://example.com/"
String linkText = link.text(); // "example""

String linkOuterH = link.outerHtml(); 
    // "<a href="http://example.com"><b>example</b></a>"
String linkInnerH = link.html(); // "<b>example</b>"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.