Java Code Examples for org.jsoup.Connection#KeyVal

The following examples show how to use org.jsoup.Connection#KeyVal . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FormElementTest.java    From jsoup-learning with MIT License 6 votes vote down vote up
@Test public void createsSubmitableConnection() {
    String html = "<form action='/search'><input name='q'></form>";
    Document doc = Jsoup.parse(html, "http://example.com/");
    doc.select("[name=q]").attr("value", "jsoup");

    FormElement form = ((FormElement) doc.select("form").first());
    Connection con = form.submit();

    assertEquals(Connection.Method.GET, con.request().method());
    assertEquals("http://example.com/search", con.request().url().toExternalForm());
    List<Connection.KeyVal> dataList = (List<Connection.KeyVal>) con.request().data();
    assertEquals("q=jsoup", dataList.get(0).toString());

    doc.select("form").attr("method", "post");
    Connection con2 = form.submit();
    assertEquals(Connection.Method.POST, con2.request().method());
}
 
Example 2
Source File: FormElement.java    From jsoup-learning with MIT License 6 votes vote down vote up
/**
 * Get the data that this form submits. The returned list is a copy of the data, and changes to the contents of the
 * list will not be reflected in the DOM.
 * @return a list of key vals
 */
public List<Connection.KeyVal> formData() {
    ArrayList<Connection.KeyVal> data = new ArrayList<Connection.KeyVal>();

    // iterate the form control elements and accumulate their values
    for (Element el: elements) {
        if (!el.tag().isFormSubmittable()) continue; // contents are form listable, superset of submitable
        String name = el.attr("name");
        if (name.length() == 0) continue;

        if ("select".equals(el.tagName())) {
            Elements options = el.select("option[selected]");
            for (Element option: options) {
                data.add(HttpConnection.KeyVal.create(name, option.val()));
            }
        } else {
            data.add(HttpConnection.KeyVal.create(name, el.val()));
        }
    }
    return data;
}
 
Example 3
Source File: UrlConnectTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test fetching a form, and submitting it with a file attached.
 */
@Test
public void postHtmlFile() throws IOException {
    Document index = Jsoup.connect("http://direct.infohound.net/tidy/").get();
    FormElement form = index.select("[name=tidy]").forms().get(0);
    Connection post = form.submit();

    File uploadFile = ParseTest.getFile("/htmltests/google-ipod.html");
    FileInputStream stream = new FileInputStream(uploadFile);
    
    Connection.KeyVal fileData = post.data("_file");
    fileData.value("check.html");
    fileData.inputStream(stream);

    Connection.Response res;
    try {
        res = post.execute();
    } finally {
        stream.close();
    }

    Document out = res.parse();
    assertTrue(out.text().contains("HTML Tidy Complete"));
}
 
Example 4
Source File: FormElementTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test public void createsFormData() {
    String html = "<form><input name='one' value='two'><select name='three'><option value='not'>" +
            "<option value='four' selected><option value='five' selected><textarea name=six>seven</textarea>" +
            "<input name='seven' type='radio' value='on' checked><input name='seven' type='radio' value='off'>" +
            "<input name='eight' type='checkbox' checked><input name='nine' type='checkbox' value='unset'>" +
            "<input name='ten' value='text' disabled>" +
            "</form>";
    Document doc = Jsoup.parse(html);
    FormElement form = (FormElement) doc.select("form").first();
    List<Connection.KeyVal> data = form.formData();

    assertEquals(6, data.size());
    assertEquals("one=two", data.get(0).toString());
    assertEquals("three=four", data.get(1).toString());
    assertEquals("three=five", data.get(2).toString());
    assertEquals("six=seven", data.get(3).toString());
    assertEquals("seven=on", data.get(4).toString()); // set
    assertEquals("eight=on", data.get(5).toString()); // default
    // nine should not appear, not checked checkbox
    // ten should not appear, disabled
}
 
Example 5
Source File: HttpConnection.java    From jsoup-learning with MIT License 5 votes vote down vote up
private static void writePost(Collection<Connection.KeyVal> data, OutputStream outputStream) throws IOException {
    OutputStreamWriter w = new OutputStreamWriter(outputStream, DataUtil.defaultCharset);
    boolean first = true;
    for (Connection.KeyVal keyVal : data) {
        if (!first) 
            w.append('&');
        else
            first = false;
        
        w.write(URLEncoder.encode(keyVal.key(), DataUtil.defaultCharset));
        w.write('=');
        w.write(URLEncoder.encode(keyVal.value(), DataUtil.defaultCharset));
    }
    w.close();
}
 
Example 6
Source File: FormElementTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test public void controlsAddedAfterParseAreLinkedWithForms() {
    Document doc = Jsoup.parse("<body />");
    doc.body().html("<form />");

    Element formEl = doc.select("form").first();
    formEl.append("<input name=foo value=bar>");

    assertTrue(formEl instanceof FormElement);
    FormElement form = (FormElement) formEl;
    assertEquals(1, form.elements().size());

    List<Connection.KeyVal> data = form.formData();
    assertEquals("foo=bar", data.get(0).toString());
}
 
Example 7
Source File: FormElementTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test public void usesOnForCheckboxValueIfNoValueSet() {
    Document doc = Jsoup.parse("<form><input type=checkbox checked name=foo></form>");
    FormElement form = (FormElement) doc.select("form").first();
    List<Connection.KeyVal> data = form.formData();
    assertEquals("on", data.get(0).value());
    assertEquals("foo", data.get(0).key());
}
 
Example 8
Source File: FormElementTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test public void usesOnForCheckboxValueIfNoValueSet() {
    Document doc = Jsoup.parse("<form><input type=checkbox checked name=foo></form>");
    FormElement form = (FormElement) doc.select("form").first();
    List<Connection.KeyVal> data = form.formData();
    assertEquals("on", data.get(0).value());
    assertEquals("foo", data.get(0).key());
}
 
Example 9
Source File: HttpConnection.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public Connection.KeyVal data(String key) {
    Validate.notEmpty(key, "Data key must not be empty");
    for (Connection.KeyVal keyVal : request().data()) {
        if (keyVal.key().equals(key))
            return keyVal;
    }
    return null;
}
 
Example 10
Source File: HttpConnection.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public Connection.KeyVal data(String key) {
    Validate.notEmpty(key, "Data key must not be empty");
    for (Connection.KeyVal keyVal : request().data()) {
        if (keyVal.key().equals(key))
            return keyVal;
    }
    return null;
}
 
Example 11
Source File: HttpConnection.java    From jsoup-learning with MIT License 5 votes vote down vote up
private Request() {
    timeoutMilliseconds = 3000;
    maxBodySizeBytes = 1024 * 1024; // 1MB
    followRedirects = true;
    data = new ArrayList<Connection.KeyVal>();
    method = Connection.Method.GET;
    headers.put("Accept-Encoding", "gzip");
    parser = Parser.htmlParser();
}
 
Example 12
Source File: HttpConnectionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test public void inputStream() {
    Connection.KeyVal kv = HttpConnection.KeyVal.create("file", "thumb.jpg", ParseTest.inputStreamFrom("Check"));
    assertEquals("file", kv.key());
    assertEquals("thumb.jpg", kv.value());
    assertTrue(kv.hasInputStream());

    kv = HttpConnection.KeyVal.create("one", "two");
    assertEquals("one", kv.key());
    assertEquals("two", kv.value());
    assertFalse(kv.hasInputStream());
}
 
Example 13
Source File: FormElementTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test public void controlsAddedAfterParseAreLinkedWithForms() {
    Document doc = Jsoup.parse("<body />");
    doc.body().html("<form />");

    Element formEl = doc.select("form").first();
    formEl.append("<input name=foo value=bar>");

    assertTrue(formEl instanceof FormElement);
    FormElement form = (FormElement) formEl;
    assertEquals(1, form.elements().size());

    List<Connection.KeyVal> data = form.formData();
    assertEquals("foo=bar", data.get(0).toString());
}
 
Example 14
Source File: FormElementTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test public void usesOnForCheckboxValueIfNoValueSet() {
    Document doc = Jsoup.parse("<form><input type=checkbox checked name=foo></form>");
    FormElement form = (FormElement) doc.select("form").first();
    List<Connection.KeyVal> data = form.formData();
    assertEquals("on", data.get(0).value());
    assertEquals("foo", data.get(0).key());
}
 
Example 15
Source File: HttpConnection.java    From jsoup-learning with MIT License 4 votes vote down vote up
public Request data(Connection.KeyVal keyval) {
    Validate.notNull(keyval, "Key val must not be null");
    data.add(keyval);
    return this;
}
 
Example 16
Source File: HttpConnection.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public Request data(Connection.KeyVal keyval) {
    Validate.notNull(keyval, "Key val must not be null");
    data.add(keyval);
    return this;
}
 
Example 17
Source File: HttpConnection.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public Collection<Connection.KeyVal> data() {
    return data;
}
 
Example 18
Source File: HttpConnection.java    From jsoup-learning with MIT License 4 votes vote down vote up
public Collection<Connection.KeyVal> data() {
    return data;
}
 
Example 19
Source File: HttpConnection.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public Request data(Connection.KeyVal keyval) {
    Validate.notNull(keyval, "Key val must not be null");
    data.add(keyval);
    return this;
}
 
Example 20
Source File: Exchangeable.java    From J-Kinopoisk2IMDB with Apache License 2.0 4 votes vote down vote up
@Override
public Connection.Request data(Connection.KeyVal keyval) {
    return null;
}