Jump to content
IltonK

Httpcliet (post) x Httpserver (Passando parametros)

Recommended Posts

Olá, 

alguem pode me ajudar, estou comecando agora no  ICS e não estou conseguindo enviar de com POST do httpclient e passando parametros, e recebendo no httpserver, no caso ele faz até a comunicacao normal porem não recebe os dados passados do parametros.

 


HTTPCLIENT:
procedure THttpPostForm.PostButtonClick(Sender: TObject);
begin
    HttpCli1.ServerAuth:=httpAuthDigest;

    Data := 'FirstName=' + UrlEncodeToA(Trim(FirstNameEdit.Text)) + '&' +  //PARAMETROS
                'LastName='  + UrlEncodeToA(Trim(LastNameEdit.Text))  + '&' +
               'Submit=Submit';
    HttpCli1.SendStream := TMemoryStream.Create;
    HttpCli1.SendStream.Write(Data[1], Length(Data));
    HttpCli1.SendStream.Seek(0, 0);
    HttpCli1.RcvdStream      := TMemoryStream.Create;
    HttpCli1.URL             := Trim(ActionURLEdit.Text);
    HttpCli1.ContentTypePost := 'application/x-www-form-urlencoded';

    StartTime := 0;
    HttpCli1.PostAsync;
end;


 


NO SERVER:
procedure TSslWebServForm.SslHttpServer1PostDocument(
    Sender    : TObject;            { HTTP server component                 }
    Client    : TObject;            { Client connection issuing command     }
    var Flags : THttpGetFlag);      { Tells what HTTP server has to do next }
var
    ClientCnx : TMyHttpConnection;
begin
    ClientCnx := Client as TMyHttpConnection;
    displaymemo.Lines.Add('POST DOCUMENT: ' + CLIENTCNX.Params);  //parametros sempre vem vazio
end.
 

o que estou fazendo de errado ?


 

Share this post


Link to post

Please translate your post to English (Use the Edit button).

Share this post


Link to post

Is "data" variable of type string? Then try with Ansistring please,

Share this post


Link to post

Sorry for sending in Portuguese.

Yes, data = Ansistring

I'm even using the examples (demo) that come in the package for testing

OverbyteIcsHttpPost = using to post

OverbyteIcsSslWebServ or OverbyteIcsBasicWebServer
I was unable to make it read the parameters in either of the two server examples.

It communicates but does not receive the parameters.

The only way it can receive them would be to pass them directly to the URL, for example: http://192.168.1.10/teste?codigo=10
In this case, it identifies the parameters

But when I send them via HttpCli1.SendStream, I get no response, it arrives empty.

Share this post


Link to post

You try to read "parameters", but send data in body!? Then you must read body of request,

Share this post


Link to post

I'm trying to read the request body but it returns an error:
var

    ClientCnx : TMyHttpConnection;
    ContentStream: TStringStream;
    ContentString: string;
begin
    ClientCnx := Client as TMyHttpConnection;
    ContentStream := TStringStream.Create;
    ContentStream.LoadFromStream(ClientCnx.DocStream);
    ContentString := ContentStream.DataString;
    ShowMessage('Conteúdo recebido: ' + ContentString);

Share this post


Link to post

I'm using it at: SslHttpServer1PostDocument error:
17:17:20:908 Handle Background Exception, source: TCustomWSocket.ASyncReceive - Access violation at address 00F194DF in module 'OverbyteIcsSslWebServ.exe' (offset B94DF). Read of address 00000000

Share this post


Link to post

I'm not getting it... but I'll keep trying, since the ICS is
for now, thank you very much, if there's any news, I'll post it here.
thank you very much

Share this post


Link to post
12 hours ago, IltonK said:

I'm not getting it...

Have a look at the demo program OverbyteIcsSslWebServ.dproj!

In OverbyuteIcsSslWebServ1.pas, look at method SslHttpServer1PostDocument(). If the posted URL is the one you expect (In the demo, this URL is '/cgi-bin/cgifrm1.exe' and no, there NO cgifrm1.exe program, this is just a kind of virtual program), you have to allocate space for the posted data and return with Flags set to hgAcceptData. Then since you accept data, you'll get one or more PostedData events (Data comes in chunks). In the demo, look at SslHttpServer1PostedData().

 

In my opinion, this demo is exactly what you want to do. Study it carefully.

 

BTW: If you don't do what I said (Edit you message to translate it in English, even in bad English. Use Google translate if needed), then you delay the answer. Now, study the sample I pointed to you, revise your code and if you still have an issue, post a NEW question, in English.

Edited by FPiette

Share this post


Link to post

Testing your own client against your own server makes debugging harder. 

 

You should test your client against the ICS multi web server sample, specifically post to https://localhost/postinfo.html which logs all URL or POST parameters passed to it, and returns them as a web page, so you know exactly what the client is sending.  The server code is in OverbyteIcsSslMultiWebUploads.pas and illustrates how to untangle parameters. 

 

For the client, you should be using the TSslHttpRest component which requires less code, look at the doHttpSimpleUploadClick function in the snippets sample, you build your parameters and then simply POST them.

 

SslHttpRest.RestParams.PContent := PContUrlEncoded;
SslHttpRest.RestParams.AddItem('FileTitle', mytitle);

StatCode := SslHttpRest.RestRequest(httpPOST, myurl, False, '');

 

You can test all this against your server using the OverbyteIcsHttpRestTst sample. 

 

Angus

 

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
×