Jump to content
miguelandradebr

Delphi 11 POST AND GET HTTP

Recommended Posts

Hello Folks!

 


<form method="post" action="https://2captcha.com/in.php" enctype="multipart/form-data">
<input type="hidden" name="method" value="post">
Your key:
<input type="text" name="key" value="6969696969696969">
The CAPTCHA file:
<input type="file" name="file">
<input type="submit" value="Upload and get the ID">
</form>

 

I'm using this code in HTML to send a image to 2captcha website to break a captcha of a HTML page

 

What is the easy and best way to work with "post "and "get" HTTP with delphi 11?

 

here is the code for the "GET" https://2captcha.com/res.php?key=696969696969&amp;action=get&amp;id=76593642581 to got the result of the captcha after 5 seconds

 

Thanks

Share this post


Link to post
Posted (edited)

Hi

 

Seems that we can works with Indy IdHTTP1

 

i'm trying to convert HTML code aobe to Delphi:

 


procedure TForm1.Button1Click(Sender: TObject);
var
  PostData: TStringList;
  rta: string;
begin
  PostData := TStringList.Create;
  try
    PostData.Add('key='+'6969696969');
    PostData.Add('file='+'C:\Users\migue\OneDrive\Desktop\form\Win32\Debug\captcha.png');


    IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';

    rta := IdHTTP1.Post('https://2captcha.com/in.php', PostData);
  finally
    PostData.Free;
  end;

  Memo1.Lines.Text := rta;
end;

 

Can someone helps?

Edited by miguelandradebr

Share this post


Link to post
Posted (edited)

Yes, you need to use TIdMultipartFormDataStream for "multipart/form-data" requests, and TStringList for "application/x-www-form-urlencoded" requests.  Your HTML uses "multipart/form-data".

 

But, the TIdHTTP code you linked to is wrong for this task, because:

  • It is sending form fields that don't exist in the HTML you showed.
  • It is using a TStringStream to hold binary data. Use TMemoryStream instead.
  • TIdMultipartFormDataStream.AddObject() is deprecated, use AddFormField() instead.
  • It is setting the wrong ContentType. It should be set to 'multipart/form-data' instead, and don't set the ContentEncoding at all. Or better, simply don't set the ContentType at all, let Post() handle that.
  • It is passing in the entire HTML <a> tag to TIdHTTP.Post().  It needs to pass in only the URL instead.

Try something more like this:

procedure TForm1.Button1Click(Sender: TObject);
var
  PostData: TIdMultipartFormDataStream;
  rta: string;
begin
  PostData := TIdMultipartFormDataStream.Create;
  try
    PostData.AddFormField('method', 'post');
    PostData.AddFormField('key', '6969696969696969');
    PostData.AddFile('file', 'C:\Users\migue\OneDrive\Desktop\form\Win32\Debug\captcha.png');
    //IdHTTP1.Request.ContentType := 'multipart/form-data';
    rta := IdHTTP1.Post('https://2captcha.com/in.php', PostData);
  finally
    PostData.Free;
  end;

  Memo1.Lines.Text := rta;
end;

 

Edited by Remy Lebeau

Share this post


Link to post
39 minutes ago, Remy Lebeau said:

Yes, you need to use TIdMultipartFormDataStream for "multipart/form-data" requests, and TStringList for "application/x-www-form-urlencoded" requests.  Your HTML uses "multipart/form-data".

 

But, the TIdHTTP code you linked to is wrong for this task, because:

  • It is sending form fields that don't exist in the HTML you showed.
  • It is using a TStringStream to hold binary data. Use TMemoryStream instead.
  • TIdMultipartFormDataStream.AddObject() is deprecated, use AddFormField() instead.
  • It is setting the wrong ContentType. It should be set to 'multipart/form-data' instead, and don't set the ContentEncoding at all. Or better, simply don't set the ContentType at all, let Post() handle that.
  • It is passing in the entire HTML <a> tag to TIdHTTP.Post().  It needs to pass in only the URL instead.

Try something more like this:


procedure TForm1.Button1Click(Sender: TObject);
var
  PostData: TIdMultipartFormDataStream;
  rta: string;
begin
  PostData := TIdMultipartFormDataStream.Create;
  try
    PostData.AddFormField('method', 'post');
    PostData.AddFormField('key', '6969696969696969');
    PostData.AddFile('file', 'C:\Users\migue\OneDrive\Desktop\form\Win32\Debug\captcha.png');
    //IdHTTP1.Request.ContentType := 'multipart/form-data';
    rta := IdHTTP1.Post('https://2captcha.com/in.php', PostData);
  finally
    PostData.Free;
  end;

  Memo1.Lines.Text := rta;
end;

 

Thank you Remy its working!!!

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

×