Search code examples
javaformsjsoupwordnet

JSOUP: set form data


I am creating a JAVA program that uses JSOUP. This is what I have in mind:

I want to use WordNet (http://wordnetweb.princeton.edu/perl/webwn). I there is a basic form there where a user can input a string. It checks if the string is a word in the English dictionary. When you submit the form the URL changes. I get this URL using. Then I check for a certain h3 tag that appears only if it is not a word.

My issue is able to fill the form and submit it using JSOUP, and getting the URL of where the submission takes me.

Here is the form

<form method="get" action="webwn" enctype="multipart/form-data" name="f">
Word to search for: 
<input type="text" name="s" maxlength="500">
<input type="submit" name="sub" value="Search WordNet">
<input type="hidden" name="o2" value="">
<input type="hidden" name="o0" value="1">
<input type="hidden" name="o8" value="1">
<input type="hidden" name="o1" value="1">
<input type="hidden" name="o7" value="">
<input type="hidden" name="o5" value="">
<input type="hidden" name="o9" value="">
<input type="hidden" name="o6" value="">
<input type="hidden" name="o3" value="">    
<input type="hidden" name="o4" value="">
<input type="hidden" name="h" value="">
</form>

I'm not sure what to do. This is the only part I am stuck on. I tried this so far but it does not do anything

public static String getUrl(String search) throws IOException{
        String url = "http://wordnetweb.princeton.edu/perl/webwn";
        Document doc = Jsoup.connect(url)
                .data("S", search)
                .data("o2", "")
                .data("o0", "1")
                .data("o8", "1")
                .data("o1", "1")
                .data("o7", "")
                .data("o5", "")
                .data("o9", "")
                .data("o6", "")
                .data("o3", "")
                .data("o4", "")
                .data("h", "")
                .post();
        String newURL = doc.location().toString();
        System.out.println(newURL);
        return (newURL);
}

Solution

  • you could just search what you want by manipulating the url's endpoint:

    http://wordnetweb.princeton.edu/perl/webwn?s= and concatenating what you want to search to it.

    if the word you're searching for was "bill" then

    /*
     * returns url of search term using jsoup
     */
    public static String getUrl(String search) throws IOException{
        String url = "http://wordnetweb.princeton.edu/perl/webwn?s=";
        Document doc = Jsoup.connect(url + search).get();
        String newURL = doc.location().toString();
        System.out.println(newURL);
        return (newURL);
    }
    

    then if you just wanted to test if a word was a word you could do this

    /*
     * returns true if is a word
     */
    public static boolean isWord(String search) throws IOException{
    
        String url = "http://wordnetweb.princeton.edu/perl/webwn?s=";
        String notAWord = "Your search did not return any results.";
    
        Document doc = Jsoup.connect(url + search).get();
        String searchH3 = doc.select("h3").text();
    
        return searchH3.contains(notAWord) ? false : true;
    
    }
    

    if you want to make the post request however you could do it by using Connection.Response to get the response of your post. You just need to have import org.jsoup.Connection as an import to use the Connection API.

    public static String getUrl(String search) throws IOException{
    
        Connection.Response wordForm = Jsoup.connect("http://wordnetweb.princeton.edu/perl/webwn")
                .method(Connection.Method.GET)
                .execute();
    
        String url = "http://wordnetweb.princeton.edu/perl/webwn";
        Document doc = Jsoup.connect(url)
                .data("s", search)
                .data("o2", "")
                .data("o0", "1")
                .data("o8", "1")
                .data("o1", "1")
                .data("o7", "")
                .data("o5", "")
                .data("o9", "")
                .data("o6", "")
                .data("o3", "")
                .data("o4", "")
                .data("h", "")
                .cookies(wordForm.cookies())
                .post();
    
        System.out.println(doc);
    
        String newURL = doc.location().toString();
        return (newURL);
    }
    

    A similar example can be found here