RTC Forums
November 10, 2024, 08:24:23 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: Dummie Question: How to post a simple request?  (Read 6048 times)
kaju74
Newbie
*
Posts: 25


« on: September 30, 2010, 09:02:22 PM »

Hi...

After understanding the basics in C/S developing, I'm fail with a simple http-post.

This is, would the header should looks like:

Quote
POST /tclrega.exe HTTP/1.1
User-Agent: Java/1.6.0_18
Host: homematic.w:8181
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-type: application/x-www-form-urlencoded
Content-Length: 51     

Write(dom.GetObject("Gasverbrauch").State("4711"));

But I didn't get it to work...

Can u help me?

Thank you,
Marc
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #1 on: September 30, 2010, 09:18:29 PM »

If you haven't already done so, please go through Quick Start lessons.

The most important one for you now is Client Lesson 1.

Also, for your specific requirement, take a look at the "QuickStart\ClientUpload" and "QuickStart\BrowserUpload" projects from the RTC SDK package. One shows you how to upload files to a WebServer, the other shows you how to send any kind of data to a RTC Server.

If you are unsure about how to set HTTP header variables, here is the code which would set all the request header variables you have listed in your post. You can either prepare request headers before the Post method from the main thread, or from the OnBeginRequest event of the TRtcDataRequest component like here:

procedure TMyForm.MyDataRequestBeginRequest(Sender:TRtcConnection);
var cli:TRtcDataClient;
  begin
  cli:=TRtcDataClient(Sender);
  cli.Request.Method:='POST';
  cli.Request.FileName:='/tclrega.exe';
  cli.Request['User-Agent']:='Java/1.6.0_18';
  cli.Request['Host']:='homematic.w';
  cli.Request['Accept']:='text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2';
  cli.Request['Content-type']:='application/x-www-form-urlencoded';

  // Now you can either manually set "Request.ContentLength" and
  // then send the content out from OnDataSent events, or ...
  // if the content is relatively short, you can simply write the complete
  // content using the Write() method here without setting ContentLength:
  cli.Write(...);
  end;

I hope that's what you were looking for. If you need more info, please let me know.

Best Regards,
Danijel Tkalcec
Logged
kaju74
Newbie
*
Posts: 25


« Reply #2 on: October 02, 2010, 04:05:45 PM »

Hi..

That's exactly what I've searched for and it works fine, but - one more question:

Is there a chance to use the same mechanism knowing from "Execute..." to do all
the stuff in just one method (w/o implementing the request events?).

This is, what my code currently looks like:

Code:
 with RtcDataRequest1 do
    begin
      Request.Info['script'] := Src.DataString;

      Request.Method:='POST';
      Request.FileName:='/tclrega.exe';
      Request['User-Agent']:='Java/1.6.0_18';
      Request['Host']:='homematic.w';
      Request['Accept']:='text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2';
      Request['Content-type']:='application/x-www-form-urlencoded';

      Post;

      rtcHTTPClient.Write(Src.DataString);
    end;
  end;

And the "DataReceived" event:

Code:
procedure TFormMain.RtcDataRequest1DataReceived(Sender: TRtcConnection);
begin
  with TRtcDataClient(Sender) do
    if Response.Done then
      ShowMessage(Read);
end;

So, any way to save this method a wait for completition in the caller method?

Regards,
Marc
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #3 on: October 02, 2010, 04:49:42 PM »

The Execute method is only for making blocking remote functions calls. You can use the "WaitForCompletion" method with TRtcDataRequest components to wait for responses from posted requests in blocking mode, but you still need to implement events.

Also ... you should ONLY make Write() and Read() calls from inside events triggered by the RTC SDK. NEVER call Read() or Write() methods directly from the main thread like you did in the code above. Doing so could easily result in unexpected behavior in single-threaded mode and will inevitably end up in Access Violations in multithreaded mode. Remember to ONLY make Write() and Read() calls from within events triggered by the RTC SDK.

The correct implementation would be ...

1) In the main thread, prepare and post the request:

with RtcDataRequest1 do
    begin
      Request.Info['script'] := Src.DataString;

      Request.Method:='POST';
      Request.FileName:='/tclrega.exe';
      Request['User-Agent']:='Java/1.6.0_18';
      Request['Host']:='homematic.w';
      Request['Accept']:='text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2';
      Request['Content-type']:='application/x-www-form-urlencoded';

      Post;
    end;
  end;

2) In the OnBeginRequest event, write the content body out:

with TRtcDataClient(Sender) do
  Write(Request.Info['script']);

3) In the OnDataReceived event, get the response:

 with TRtcDataClient(Sender) do
    if Response.Done then
      ShowMessage(Read);

Best Regards,
Danijel Tkalcec
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.024 seconds with 17 queries.