Jump to content
Carlos Tré

Filling and submitting a form programmatically with ICS

Recommended Posts

Dear All,

 

I need to fill two fields in a form and then submit it. As I'm not well versed in this area, I come here and for some guidance.

The page where they sit is coded as follows:

 
<html>
<body>
<form id="form1" name="form1" action="envia_arq2.php" method="post">
	
Senha: <input  TYPE="password" NAME="senha" SIZE="12"><br /><br />
Seu nome: <input  TYPE="text" NAME="nome" SIZE="12">
<input name="enviar" type="submit" value="Enviar">
</form>
</body>
</html>

This is a FMX application, so I can't use TWebBrowser as I've found in several articles in the internet.

Furthermore, even the VCL version I couldn't make work, after navigating to the page the Document property is nil, making it impossible to retrieve the fields as indicated in those articles.

 

I then moved on to ICS TSslHttpCli, tried a few basic things, but I came up empty on the very first attempt where I just set the URL (https://www.trivial.com.br/envia_arq2.htm) and did a GET to try and see how to proceed from there. An exception is raised signaling a missing SSL Context. Perhaps this is not the way to go.

 

Could you please kindly share some light on this?

 

Thanks in advance.

 

-- Carlos

 

 

 

Share this post


Link to post

You need to look at your form. "Action" is the URL you have to use to submit your form. "Method" is the way to submit if: Get or Post which maps directly to HTTP component. Then you have to look at all "input" tags to find the name of the items to send. Then you have to format a buffer with name/values pair and submit it.

 

François Piette

 

  • Like 1

Share this post


Link to post

Also use TSslHttpRest instead of TSslHttpCli since this avoids you needing to use an SslContext in your application, look at the OverbyteIcsHttpRest sample.

 

Angus

 

  • Like 1

Share this post


Link to post
10 hours ago, FPiette said:

You need to look at your form. "Action" is the URL you have to use to submit your form. "Method" is the way to submit if: Get or Post which maps directly to HTTP component. Then you have to look at all "input" tags to find the name of the items to send. Then you have to format a buffer with name/values pair and submit it.

 

7 hours ago, Angus Robertson said:

Also use TSslHttpRest instead of TSslHttpCli since this avoids you needing to use an SslContext in your application, look at the OverbyteIcsHttpRest sample.

Thanks to both of you for your input, very much appreciated. I'll comeback and post the results.

 

-- Carlos

Share this post


Link to post
12 hours ago, FPiette said:

You need to look at your form. "Action" is the URL you have to use to submit your form. "Method" is the way to submit if: Get or Post which maps directly to HTTP component. Then you have to look at all "input" tags to find the name of the items to send. Then you have to format a buffer with name/values pair and submit it.

Also, it is common for webservers to use cookies with webforms, so you should first GET the webpage that contains the webform so that you download any initial cookies, then parse the webform fields (ie, in case any contain randomly generated field values), then submit the webform as directed.  Also, make sure you submit ALL <input> fields that have a non-empty value - including the submit button itself!  Sometimes webforms have multiple buttons, in which case the webserver needs to know which one is performing the submission.

  • Like 1

Share this post


Link to post
1 hour ago, Remy Lebeau said:

Also, make sure you submit ALL <input> fields that have a non-empty value - including the submit button itself!  Sometimes webforms have multiple buttons, in which case the webserver needs to know which one is performing the submission.

Thank you very much for input, very much appreciated.

 

How would I encode the submit button? Considering the code snippet

input name="enviar" type="submit" value="Enviar">

the URL encoded parameters shall be "senha=violeta&nome=tre&enviar=Enviar"

(https://www.trivial.com.br/envia_arq2.htm?senha=violeta&nome=tre&enviar=Enviar)?

 

-- Carlos

 

 

Share this post


Link to post
5 hours ago, Carlos Tré said:

How would I encode the submit button? Considering the code snippet


input name="enviar" type="submit" value="Enviar">

The same way you encode any other <input> field - as a "name=value" pair, where the "name" and "value" are properly url-encoded.  The "type" of the <input> field doesn't matter.

Quote

the URL encoded parameters shall be "senha=violeta&nome=tre&enviar=Enviar"

Yes.

Quote

(https://www.trivial.com.br/envia_arq2.htm?senha=violeta&nome=tre&enviar=Enviar)?

The <form> in question specifies "method=post", so you would need to send the encoded parameters in a POST body, not in the query component of a URL, eg:

POST /envia_arq2.htm HTTP/1.1
Host: www.trivial.com.br
Content-Type: application/x-www-form-urlencoded
Content-Length: 36
Connection: close

senha=violeta&nome=tre&enviar=Enviar

However that translates to TSslHttpCli/TSslHttpRest (I don't use ICS).

Edited by Remy Lebeau
  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×