Hi,
I try to send data to website with POST method wtih my Aruino to activate a Javascript function (Function sb(val)), which corresponds to a click on a button on the web page.
A user agent -- usually a browser, but here an Arduino -- makes a request to a web server. This request is usually a GET, but could be a POST, DELETE, etc
The web server may respond with HTML
The user agent will parse this HTML, which in this case contains at least one <form> and some <script>, which are converted into the corresponding things in the DOM and the function in JavaScript
The user agent will trigger the function due to the user interaction with the <button>
You can't trigger JavaScript in a page if you don't have a JavaScript engine, for a page you did not receive and parse to build a DOM.
But based on what the sb function actually does, you might be able to do the equivalent without any JavaScript at all.
Look at the HTML and find the first (likely only) <form> -- first because the JavaScript said form[0]. Check for two attributes
method: if it's missing, it defaults to get, but it might be post
action: if it's missing, it's the current page's path, but could be something else starting with /
Possibly it has method="post" and no action, in which case you were close. But val is the variable name in the function in JavaScript and has no relation to what the web server expects. The form element -- between the form[0]. and .value is answer, so try sending
POST / HTTP/1.1
Host: 10.105.154.258
Content-Type: application/x-www-form-urlencoded
Content-Length: 8
answer=1
Note the blank line between the headers and the body, which you did not have. And strictly speaking, you don't need a line break after the body with println -- you say how many bytes to expect -- but it probably won't hurt.
The value of answer need to be encoded. And if it's a GET instead, the values go in the URL as query parameters.
this is part of the HTTP header fields and should be sent BEFORE the empty line
"data" is the last you want send
don't add println at the end - this will transmit two more bytes, and as you have told the server the length of data it makes no sense to send more.
but I guess your main problem is the content of data. What's in?
IP: 10.105.158.254 is a firewall disclaimer page with a button that accepts the conditions. The purpose of arduino code is to simulate the click of a button to access the internet and send an email from an SMTP server.
This will be difficult to get working. What many people do on Arduino is "manually perform HTTP" by printing the headers and such to do one-off requests and grab the response. But the way this type of firewall works generally is another layer on top of that; essentially "manually perform like a browser"
In addition to answer, the form has two other hidden fields; you'll need to send those also, and the value that is an URL will have to be encoded. The response you receive may contain a cookie, designated with a Set-Cookie header in the response. You'll need to parse and store the cookie, and send it for any request that match its parameters.
The response will also likely have a redirect, which may contain some other unique identifying code. That may mean parsing the HTML. The response for the firewall or the original may contain other JavaScript, and that code might actually do something that is not as easy to replicate without actually executing -- or being simulated as -- JavaScript.
But first, you should be getting some kind of actual HTTP response to the POST, not -1. Try posting your whole sketch here, simplified and with any secrets redacted. Also include the responses as code too, not as a screenshot.