Recent

Author Topic: INDY HTTP  (Read 330 times)

pcurtis

  • Sr. Member
  • ****
  • Posts: 384
INDY HTTP
« on: November 19, 2020, 11:57:49 am »
Hi All,

How would I implement this

Code: Pascal  [Select][+][-]
  1.     HTTPSend := THTTPSend.Create;
  2.     try
  3.       HTTPSend.Useragent := 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)';
  4.       HTTPSend.MimeType  := 'application/x-www-form-urlencoded';
  5.  
  6.       WriteStrToStream(HTTPSend.Document,
  7.             ansistring('vb_login_username=someuser' +
  8.                        '&vb_login_password=' +
  9.                        '&s=' +
  10.                        '&securitytoken=guest' +
  11.                        '&do=login' +
  12.                        '&vb_login_md5password=asdfghjkjhgfdsdfghjkjhgfdsdfgbn' +
  13.                        '&vb_login_md5password_utf=asdfghjkjhgfdsdfghjkjhgfdsdfgbn'));;
  14.  
  15.       Ok := HTTPSend.HTTPMethod('POST', 'http://somesite.com/login.php?do=login');
  16.  
  17.       TextFromDocument := ReadStrFromStream(HTTPSend.Document, HTTPSend.Document.Size);
  18.  
  19.       Memo1.Lines.Add('---------------');
  20.       Memo1.Lines.Add('Result = ' + HTTPSend.ResultCode.ToString + ' ' + HTTPSend.ResultString);
  21.       Memo1.Lines.Add('---------------');
  22.       Memo1.Lines.Add(HTTPSend.Headers.Text);
  23.       Memo1.Lines.Add('---------------');
  24.      // Memo1.Lines.Add(TextFromDocument);
  25.       Memo1.Lines.Add('---------------');
  26.       Memo1.Lines.Add(HTTPSend.Cookies[0]);
  27.       Memo1.Lines.Add(HTTPSend.Cookies[1]);
  28.       Memo1.Lines.Add(HTTPSend.Cookies[2]);
  29.       Memo1.Lines.Add(HTTPSend.Cookies[3]);
  30.       MyC := HTTPSend.Cookies.Text;
  31.       HTTPSend.Free;
  32.  

using INDY?

Thanks in advance.
Windows 10 / Linux Mint 20
Laz 2.10.0
FPC 3.2.0

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 854
    • Lebeau Software
Re: INDY HTTP
« Reply #1 on: November 19, 2020, 06:34:30 pm »
Like this:

Code: [Select]
HTTP := TIdHTTP.Create;
try
  HTTP.Request.UserAgent := 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)';

  Params := TStringList.Create;
  try
    Params.Add('vb_login_username=someuser');
    Params.Add('vb_login_password=');
    Params.Add('s=');
    Params.Add('securitytoken=guest');
    Params.Add('do=login');
    Params.Add('vb_login_md5password=asdfghjkjhgfdsdfghjkjhgfdsdfgbn');
    Params.Add('vb_login_md5password_utf=asdfghjkjhgfdsdfghjkjhgfdsdfgbn');
     
    TextFromDocument := HTTP.Post('http://somesite.com/login.php?do=login', Params);
  finally
    Params.Free;
  end;
     
  Memo1.Lines.Add('---------------');
  Memo1.Lines.Add('Result = ' + HTTP.ResponseCode.ToString + ' ' + HTTP.ResponseText);
  Memo1.Lines.Add('---------------');
  Memo1.Lines.AddStrings(HTTP.Response.RawHeaders);
  Memo1.Lines.Add('---------------');
  // Memo1.Lines.Add(TextFromDocument);
  Memo1.Lines.Add('---------------');

  // either:
  with HTTP.CookieManager.CookieCollection do
  begin
    Memo1.Lines.Add(Cookies[0].CookieText);
    Memo1.Lines.Add(Cookies[1].CookieText);
    Memo1.Lines.Add(Cookies[2].CookieText);
    Memo1.Lines.Add(Cookies[3].CookieText);
  end;

  // or:
  with HTTP.CookieManager.CookieCollection.LockCookieList(caRead) do
  try
    Memo1.Lines.Add(Cookies[0].CookieText);
    Memo1.Lines.Add(Cookies[1].CookieText);
    Memo1.Lines.Add(Cookies[2].CookieText);
    Memo1.Lines.Add(Cookies[3].CookieText);
  finally
    HTTP.CookieManager.CookieCollection.UnlockCookieList(caRead);
  end;

  // there is no direct equivalent to this statement in Indy:
  //
  //MyC := HTTP.Cookies.Text;
  //
  // but, you can produce such a string manually, by iteration through the
  // CookieManager reading the individual TIdCookie.ClientCookie property
  // values, eg:

  // either:
  MyC := '';
  with HTTP.CookieManager.CookieCollection do
  begin
    for I := 0 to Count-1 do
      MyC := Cookies[I].ClientCookie + sLineBreak;
  end;

  // or:
  MyC := '';
  with HTTP.CookieManager.CookieCollection.LockCookieList(caRead) do
  try
    for I := 0 to Count-1 do
      MyC := Cookies[I].ClientCookie + sLineBreak;
  finally
    HTTP.CookieManager.CookieCollection.UnlockCookieList(caRead);
  end;

finally
  HTTP.Free;
« Last Edit: November 19, 2020, 06:36:12 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

pcurtis

  • Sr. Member
  • ****
  • Posts: 384
Re: INDY HTTP
« Reply #2 on: November 19, 2020, 07:23:55 pm »
Thanks. I'll take a look tomorrow.
Windows 10 / Linux Mint 20
Laz 2.10.0
FPC 3.2.0

 

TinyPortal © 2005-2018