Write to a website using java

2.2k views Asked by At

I am currently working on reading and writing to a website, only using Java. I have figured out how to read from the website, to get all the content on the actual website. But I am not completely sure where to begin with the writing, although it seems as if it is a simple action I want to do. I just want to write a specific string to a text area on a website.

To show the code I have for reading the site, I have attached it below.

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;

import org.apache.commons.io.IOUtils;

public class Read {

    public static void main(String[] args) throws IOException {

        URL url = new URL("http://website.com/");
        URLConnection con = url.openConnection();
        InputStream in = con.getInputStream();
        String encoding = con.getContentEncoding();
        encoding = encoding == null ? "UTF-8" : encoding;
        String body = IOUtils.toString(in, encoding);
        System.out.println(body);

        PrintWriter pToDocu = new PrintWriter("readText.txt");
        pToDocu.println(body);
        pToDocu.close();

    }

}

Now what I am trying to achieve is how to write the content of a string variable to a text area of which is on a website I have created. It is probably also pretty important to note that, for the info in the text area to be submitted a button called submit on the site has to be clicked.

2

There are 2 answers

1
Hari Krishna On BEST ANSWER

Use Selenium tool to write data to websites. For example this link explains, how to populate username and password fields of login form.

1
Abhishek On

I think Selenium suits best for your requirement. You can refer this tutorial.