Sunday, 16 April 2017

How to use delphi TRtcDataRequest API send http request to nodejs express server

the delphi client code, using TRtcDataRequest API to send http request to server:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, rtcDataCli, rtcInfo, rtcConn, rtcHttpCli,HttpApp;

type
  TForm1 = class(TForm)
    RtcHttpClient1: TRtcHttpClient;
    RtcDataRequest1: TRtcDataRequest;
    Button1: TButton;
    Memo1: TMemo;
    Memo2: TMemo;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    procedure Button1Click(Sender: TObject);
    procedure RtcDataRequest1BeginRequest(Sender: TRtcConnection);
    procedure RtcDataRequest1DataReceived(Sender: TRtcConnection);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    function UnicodeToAnsi(str:string):ansistring;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  RtcHttpClient1.Request.Method:='POST';
  if RadioButton1.Checked then
    RtcDataRequest1.Request.FileName:='/CheckTicket'
  else if RadioButton2.Checked then
    RtcDataRequest1.Request.FileName:='/CheckIDCard';
  RtcDataRequest1.Request.Host:=RtcHttpClient1.ServerAddr; // Set the "Host" HTTP header
  RtcDataRequest1.Post(); // Post the request to the request queue
end;

procedure TForm1.RtcDataRequest1BeginRequest(Sender: TRtcConnection);
var
    Cli:TRtcDataClient absolute Sender;
begin
  Cli.Request.ContentType:='application/Json;charset=utf-8';
  Cli.Write(Memo2.Text);
  //Cli.Write('{"license": "ÏæH92319","colorValue": 0,"colorType": 2,"type": 3}');
end;

procedure TForm1.RtcDataRequest1DataReceived(Sender: TRtcConnection);
var
  Cli:TRtcDataClient absolute Sender;
  mycontent:RtcString;
  strReturn:string;

begin
if Cli.Response.Done then
  begin
  mycontent:=Cli.Read;
  Memo1.Lines.Add('״̬Âë:'+ IntToStr(Cli.Response.StatusCode)+' '+Cli.Response.StatusText+ #13#10
                   + mycontent);

  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  RadioButton1.Checked:=True;
end;

function TForm1.UnicodeToAnsi(Str: String): AnsiString;
var
  Len: integer;
begin
  Len := Length(Str) * 2 + 1; // one for #0
  SetLength(Result, Len);
  Len := WideCharToMultiByte(CP_ACP, 0, PWideChar(Str), -1,
    PAnsiChar(Result), Len, nil, nil);
  SetLength(Result, Len - 1);
end;

end.

and on the server, I use nodejs express to listen http request:

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  console.log('----------------------------------')
  res.render('index', { title: 'Express' });
});

router.post('/', function (req, res) {
  console.log(req.body);
  console.log('post----------------------------------')
  res.send('ok')
})

module.exports = router;

The Problem is I couldn't get any response on the nodejs server side. no 404 or else reponse infomation.



via Jack Hu

No comments:

Post a Comment