onde responder uma requisição IdHTTPServer ?
11/04/2019
0
boa tarde amigos, meu problema e o seguinte :
configurei o componente IdHTTPServer para responder pegar requisição vinda de uma catraca eletronica no evento ongetcommander e a te ae tudo bem leio a informção, porem preciso enviar um result pra catraca, em qual envento eu colocaria o retorno do webser para a catraca ?
nunca tinha trabalhado com webser antes e realmente sou bem leigo nessa parte, desde ja agradeço a quem puder me ajudar.
ex q o fornecedor da catraca me mandou :
O servidor terá que implementar um webserver que responde à esta requisição. Abaixo lhe envio um exemplo de requisição:
Requisição da cartaca para o webserver
CLIENT -> SERVER
POST /new_user_identified.fcgi?session=gJgOHkblq69Vc3WLOOdUMvrf& HTTP/1.1
Host: 192.168.2.121
Accept: */*
Content-Type: application/x-www-form-urlencoded
Connection: Keep-Alive
Content-Length: 105
event=8&device_id=468507&identifier_id=1651076864&portal_id=1&user_id=1&user_name=Danilo&user_has_image=0
Resposta:
SERVER -> CLIENT
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Date: Thu, 18 Feb 2016 16:20:45 GMT
Server: lighttpd/1.4.35
{"result": {"event":7, "user_id":1, "user_name":"Danilo", "user_image":false, "portal_id":1, "actions":[ { action: "catra", parameters: "allow=both" } ] }}
Note, que o servidor não inicia esta comunição.
a catraca é uma controlID, infelizmente ela não tem exemplo em delphi, ja fiz toda a parte de ler a requisição que vem dela, identifico o usuario e preciso dar o retorno para a requisição da catraca o retorno é o que esta no ex. a cima, porem não sei onde colocar, estou lendo a requisição da seguinte forma no evento oncommandget do IdHTTPServer1 :
Código:
procedure TFormportaria.IdHTTPServer1CommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
Var
contador: Integer;
Cmd, DEPENDENTE: String;
Argumentos: TArguments;
ServerMethod1: TServerMethods1;
JSONStr: string;
lJSO: ISuperObject;
lRequest: TStringStream;
lResponse: String;
st: TStringStream;
begin
contador := 0;
novousuario:=novousuario+1;
Cmd := ARequestInfo.RawHTTPCommand;
if (UpperCase(COPY(Cmd, 1, 4)) = 'POST') then
Begin
if novousuario=1 then begin
novousuario:=novousuario+1;
Argumentos := TServerUtils.ParseRESTURL(ARequestInfo.URI);
ServerMethod1 := TServerMethods1.Create(nil);
Try
LoglastRequest(ARequestInfo);
If UpperCase(COPY(Cmd, 1, 4)) = 'POST' Then
BEGIN
IF COPY(Cmd, 1, 25) = ('POST /new_user_identified') then
begin
pID := ARequestInfo.params.Values['user_id'];
pUsuario := ARequestInfo.params.Values['user_name'];
// Edit2.Text := pID;
if strtoint(pID) > 100001 then
BEGIN
DEPENDENTE := COPY(pID, length(pID) - 5, length(pID));
Edit1.text := StrZero(DEPENDENTE, 5);
END
ELSE
Edit1.text := StrZero(pID, 5);
if strtoint(Edit1.text) > 100001 then
Edit1.text := COPY(Edit1.text, (length(trim(Edit1.text)) - 4), 9);
buscamatricula;
end;
END;
AResponseInfo.ContentText := JSONStr;
LogLastResponse(AResponseInfo);
AResponseInfo.WriteContent;
Finally
ServerMethod1.Free;
End;
end;
if novousuario=2 then novousuario:=0;
end;
end;
configurei o componente IdHTTPServer para responder pegar requisição vinda de uma catraca eletronica no evento ongetcommander e a te ae tudo bem leio a informção, porem preciso enviar um result pra catraca, em qual envento eu colocaria o retorno do webser para a catraca ?
nunca tinha trabalhado com webser antes e realmente sou bem leigo nessa parte, desde ja agradeço a quem puder me ajudar.
ex q o fornecedor da catraca me mandou :
O servidor terá que implementar um webserver que responde à esta requisição. Abaixo lhe envio um exemplo de requisição:
Requisição da cartaca para o webserver
CLIENT -> SERVER
POST /new_user_identified.fcgi?session=gJgOHkblq69Vc3WLOOdUMvrf& HTTP/1.1
Host: 192.168.2.121
Accept: */*
Content-Type: application/x-www-form-urlencoded
Connection: Keep-Alive
Content-Length: 105
event=8&device_id=468507&identifier_id=1651076864&portal_id=1&user_id=1&user_name=Danilo&user_has_image=0
Resposta:
SERVER -> CLIENT
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Date: Thu, 18 Feb 2016 16:20:45 GMT
Server: lighttpd/1.4.35
{"result": {"event":7, "user_id":1, "user_name":"Danilo", "user_image":false, "portal_id":1, "actions":[ { action: "catra", parameters: "allow=both" } ] }}
Note, que o servidor não inicia esta comunição.
a catraca é uma controlID, infelizmente ela não tem exemplo em delphi, ja fiz toda a parte de ler a requisição que vem dela, identifico o usuario e preciso dar o retorno para a requisição da catraca o retorno é o que esta no ex. a cima, porem não sei onde colocar, estou lendo a requisição da seguinte forma no evento oncommandget do IdHTTPServer1 :
Código:
procedure TFormportaria.IdHTTPServer1CommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
Var
contador: Integer;
Cmd, DEPENDENTE: String;
Argumentos: TArguments;
ServerMethod1: TServerMethods1;
JSONStr: string;
lJSO: ISuperObject;
lRequest: TStringStream;
lResponse: String;
st: TStringStream;
begin
contador := 0;
novousuario:=novousuario+1;
Cmd := ARequestInfo.RawHTTPCommand;
if (UpperCase(COPY(Cmd, 1, 4)) = 'POST') then
Begin
if novousuario=1 then begin
novousuario:=novousuario+1;
Argumentos := TServerUtils.ParseRESTURL(ARequestInfo.URI);
ServerMethod1 := TServerMethods1.Create(nil);
Try
LoglastRequest(ARequestInfo);
If UpperCase(COPY(Cmd, 1, 4)) = 'POST' Then
BEGIN
IF COPY(Cmd, 1, 25) = ('POST /new_user_identified') then
begin
pID := ARequestInfo.params.Values['user_id'];
pUsuario := ARequestInfo.params.Values['user_name'];
// Edit2.Text := pID;
if strtoint(pID) > 100001 then
BEGIN
DEPENDENTE := COPY(pID, length(pID) - 5, length(pID));
Edit1.text := StrZero(DEPENDENTE, 5);
END
ELSE
Edit1.text := StrZero(pID, 5);
if strtoint(Edit1.text) > 100001 then
Edit1.text := COPY(Edit1.text, (length(trim(Edit1.text)) - 4), 9);
buscamatricula;
end;
END;
AResponseInfo.ContentText := JSONStr;
LogLastResponse(AResponseInfo);
AResponseInfo.WriteContent;
Finally
ServerMethod1.Free;
End;
end;
if novousuario=2 then novousuario:=0;
end;
end;
Marcio Batista
Curtir tópico
+ 0
Responder
Clique aqui para fazer login e interagir na Comunidade :)