java – Post request with data to form with Jsoup library

Question:

Task : transfer data to the site, namely: send a message to the user.

Example : authorization followed by a transition to the page for parsing data under the user:

Connection.Response res = Jsoup.connect("http://адрес/user/login")
    .data("email", edt_email.getText().toString())
    .data("pass", edt_pass.getText().toString())
    .method(Method.POST)
    .execute();
Document doc = res.parse();
sessionId = res.cookie("bffssu");
Log.i("idSession", sessionId.toString());
Document doc2 = Jsoup.connect("http://адрес")
    .userAgent("Mozzila")
    .cookie("bffssu", sessionId)
    .get();
el_get_trulyalya = doc2.select("div.j-list");

Next: you need to send a message to the user. The form for sending a message in html:

<form class="i-imailDialog-form form bff_ajax_iframe_inited" method="POST" action="http://адрес/user/messages?i=vadumo" id="j-my-chat-form" enctype="multipart/form-data" target="bff_ajax_iframe226">
    <input type="hidden" name="act" value="send">
    <input type="hidden" name="i" value="vadumo">

    <div class="form-group">
        <textarea name="message" rows="4" class="form-control" placeholder="Сообщение"></textarea>
    </div>

    <button class="btn btn-sm btn-primary">Отправить</button>
    <input type="hidden" name="MAX_FILE_SIZE" value="5242880">
    <input type="file" name="attach" class="j-upload-file hidden">

    <button class="btn btn-sm btn-default pull-right j-upload-file-btn">Прикрепить файл</button>
    <div class="pull-right j-file-name hidden">
        <span class="j-name"></span> <a href="#" class="link-delete j-upload-delete"><i class="fa fa-times"></i></a>
    </div>
    <div class="clearfix"></div>
    <div class="help-block text-right mrgb0">Максимальный размер файла - 5 МБ</div>
    <input type="hidden" name="hash" value="4ee5104221f47408cd"></form>

Answer:

Perhaps the server side is explicitly expecting multipart. You can send a request in this mode without sending the file itself as follows

Document doc = Jsoup.connect("http://адрес/user/messages?i=vadumo")
        .header("Content-Type", "multipart/form-data")
        .userAgent("Mozilla")
        .data("message","Test")
        .post();
Scroll to Top