Recent

Author Topic: LNET -> Sending data of form...  (Read 12281 times)

OnixJr

  • Full Member
  • ***
  • Posts: 172
    • http://www.brlazarus.kit.net
LNET -> Sending data of form...
« on: December 22, 2007, 10:14:19 pm »
Hello...

I would like to know if it is possible to send information from a web form with the component LNET.

I would like to access page of sites that require log (scripts in PHP and ASP) but, before, I should login the user with its data login and password (the forms contains no captcha).
Portal Lazarus Brasil - http://lazaruspascal.codigolivre.org.br/portal.php
Lazarus BOOK (in portuguese) - http://lazarus-book.blogspot.com
<hipernetjr@yahoo.com.br> - Curitiba/Brazil

Almindor

  • Sr. Member
  • ****
  • Posts: 412
    • http://www.astarot.tk
RE: LNET -> Sending data of form...
« Reply #1 on: December 22, 2007, 10:23:10 pm »
You mean using lNet in a CGI program, or using the HTTP server from lNet? I'm not exactly sure what you mean. Do you want to send data to some foreign (eg: not yours) webpage?

Sending FORM data is AFAIK just the right combination of strings so I think it should be possible, but since I didn't do the HTTP part, I'm not entirely sure. The TLHttpClient should allow it, or at least you should be able to extend it for that purpose.

OnixJr

  • Full Member
  • ***
  • Posts: 172
    • http://www.brlazarus.kit.net
RE: LNET -> Sending data of form...
« Reply #2 on: December 22, 2007, 10:39:36 pm »
Almindor,

I'm using HTTPClient component... (I need send data to external sites)
when I want send data to a CGI program, I do: http://127.0.0.1/cgi-bin/cgi.exe?data=xxx
But I must complete a form (as if the user were by logging on a website) in PHP or ASP...
In delphi, I can complete the form by Internet Explorer, and TWebbrowser would be "automatically" logged...
Portal Lazarus Brasil - http://lazaruspascal.codigolivre.org.br/portal.php
Lazarus BOOK (in portuguese) - http://lazarus-book.blogspot.com
<hipernetjr@yahoo.com.br> - Curitiba/Brazil

Dr.Theopolis

  • Jr. Member
  • **
  • Posts: 69
Send POST data
« Reply #3 on: December 23, 2007, 12:07:19 am »
Form data can be encoded as GET or POST parameters, and they are usually POST.

To send GET parameters with lnet is very straightforward - with method property set to hmGet, you include your parameters in the URI property, for example: /index.php?var1=blah&var2=blah

To send POST parameters, one would think you could set method to hmPost and then set up your parameters with some kind of function calls. As far as I can tell, lnet does not fully support sending POST parameters. HOWEVER, for my own purposes, I hacked the code so that when method is hmPost, the parameters you specified in the URI will get converted to POST data before the http request is sent to the web server. If that is what you are looking for, let me know and I will look at the changes I made and  tell you what they are.

Now...if your web page accepting these parameters must be accessed with https, that is an additional requirement which I don't know if lnet can meet. I have not gotten into https with lnet yet.

OnixJr

  • Full Member
  • ***
  • Posts: 172
    • http://www.brlazarus.kit.net
RE: Send POST data
« Reply #4 on: December 23, 2007, 01:45:25 am »
Hi Dr. Theopolis...

I'm trying send POST parameter...
I have tested in phpBB foruns...
I changed the method property do hmGet and try access this url:
http://www.phpbbrasil.com.br/phpBB/ucp.php?mode=login&username=junior50&password=xxx

A page error must be showed, but the page that is showed is the login page...
I get in source code a variable sid (a hash I believe) and sent too, but the result is same...
Portal Lazarus Brasil - http://lazaruspascal.codigolivre.org.br/portal.php
Lazarus BOOK (in portuguese) - http://lazarus-book.blogspot.com
<hipernetjr@yahoo.com.br> - Curitiba/Brazil

Dr.Theopolis

  • Jr. Member
  • **
  • Posts: 69
LNET -> Sending data of form...
« Reply #5 on: December 23, 2007, 03:34:08 am »
That login page encodes the form data as POST. Sometimes GET and POST are interchangeable but it depends on the php script. Most likely you cannot substitute GET for POST.

sid is the php session id. It's different each time you log in. It must be passed also in every request sent to the web server. You have to request a web page from the server in order for it to assign you an sid.

So here's what I would try first:

1. Set up http request to http://www.phpbbrasil.com.br/phpBB/ucp.php?mode=login

2. Have your code scan the html reply for the sid . Ideally you would want to check one of the parameters of the http reply header in the lnet component, (for example, Parameters[hpReferer] or something like that), but in my experience all those parameters are blank for some reason. Maybe next version will be better.

3. Set up http request again to http://www.phpbbrasil.com.br/phpBB/ucp.php?mode=login
this time passing username, password, and sid as POST parameters. Of course, lnet doesn't support that without some code hacking.

Once logged in, every additional request must also pass the same sid obtained in step 2. PHP allows the session id to be passed either GET or POST.

The following code changes were done to lnet version 0.5.8.
In unit lhttp, find the following procedure and change to the following:

Code: [Select]

procedure TLHTTPClientSocket.SendRequest;
var
  lMessage: TStringBuffer;
  lTemp: string[23];
  hasRangeStart, hasRangeEnd: boolean;
  PostData: string;
  ParamSepPos: integer;
begin
  lMessage := InitStringBuffer(504);

  { If POST, split params from URI }
  PostData := '';
  if FRequest^.Method = hmPost then
  begin
    ParamSepPos := Pos('?', FRequest^.URI);
    if ParamSepPos > 0 then
    begin
      PostData := copy(FRequest^.URI, ParamSepPos +1, length(FRequest^.URI) - ParamSepPos);
      FRequest^.URI := copy(FRequest^.URI, 1, ParamSepPos - 1);
    end
    else
      FRequest^.Method := hmGet;
  end;
 
  AppendString(lMessage, HTTPMethodStrings[FRequest^.Method]);
  AppendChar(lMessage, ' ');
  AppendString(lMessage, FRequest^.URI);
  AppendChar(lMessage, ' ');
  AppendString(lMessage, 'HTTP/1.1'+#13#10+'Host: ');
  AppendString(lMessage, TLHTTPClient(FCreator).Host);
  if TLHTTPClient(FCreator).Port <> 80 then
  begin
    AppendChar(lMessage, ':');
    Str(TLHTTPClient(FCreator).Port, lTemp);
    AppendString(lMessage, lTemp);
  end;
  AppendString(lMessage, #13#10);
  hasRangeStart := TLHTTPClient(FCreator).RangeStart <> high(qword);
  hasRangeEnd := TLHTTPClient(FCreator).RangeEnd <> high(qword);
  if hasRangeStart or hasRangeEnd then
  begin
    AppendString(lMessage, 'Range: bytes=');
    if hasRangeStart then
    begin
      Str(TLHTTPClient(FCreator).RangeStart, lTemp);
      AppendString(lMessage, lTemp);
    end;
    AppendChar(lMessage, '-');
    if hasRangeEnd then
    begin
      Str(TLHTTPClient(FCreator).RangeEnd, lTemp);
      AppendString(lMessage, lTemp);
    end;
  end;
 
  { Add content-length and content-type headers for POST }
  if FRequest^.Method = hmPost then
  begin
    AppendString(lMessage, 'Content-Length: ' + IntToStr(length(PostData)) + #13#10);
    AppendString(lMessage, 'Content-Type: application/x-www-form-urlencoded' + #13#10);
  end;

  with FHeaderOut^.ExtraHeaders do
    AppendString(lMessage, Memory, Pos-Memory);
  AppendString(lMessage, #13#10);

  { Add Post data if applicable }
  if FRequest^.Method = hmPost then
    AppendString(lMessage, PostData);
   
  AddToOutput(TMemoryOutput.Create(Self, lMessage.Memory, 0,
    lMessage.Pos-lMessage.Memory, true));
  AddToOutput(FCurrentInput);

  WriteBlock;
end;

OnixJr

  • Full Member
  • ***
  • Posts: 172
    • http://www.brlazarus.kit.net
LNET -> Sending data of form...
« Reply #6 on: December 24, 2007, 11:19:56 am »
Thanks Dr.Theopolis, this code look work here, but I get a page which sent a message: "You need enable cookies"...
Any way for to do this?
Portal Lazarus Brasil - http://lazaruspascal.codigolivre.org.br/portal.php
Lazarus BOOK (in portuguese) - http://lazarus-book.blogspot.com
<hipernetjr@yahoo.com.br> - Curitiba/Brazil

Dr.Theopolis

  • Jr. Member
  • **
  • Posts: 69
LNET -> Sending data of form...
« Reply #7 on: December 24, 2007, 08:26:05 pm »
lnet experts will have to take over from here. I don't know how much cookie support there is.

neli

  • Jr. Member
  • **
  • Posts: 86
Re: LNET -> Sending data of form...
« Reply #8 on: July 11, 2009, 12:51:42 pm »
Cookies were not supported in the sense there were explicit functions for it.

I have added in SVN a helper function to add a cookie to the headers sent to the server:

TLHTTPClient.AddCookie(const AName, AValue: string; const APath: string = '';
  const ADomain: string = ''; const AVersion: string = '0');

Good luck, it's not tested though, so try at own risk :P

 

TinyPortal © 2005-2018