Ver Mensaje Individual
  #5  
Antiguo 30-01-2012
guitarrahugo guitarrahugo is offline
Miembro
NULL
 
Registrado: ene 2012
Posts: 15
Reputación: 0
guitarrahugo Va por buen camino
Amigos, eh intentado empezar por algo más sencillo, para ver que funcione, y luego si desarrollar el programa completo que explico arriba. Estuve viendo algunos ejemplo de lo que hay en el foro y trate de armar un formulario delphi que mande a llamar una función en php y esta le devuelva un string con los mensajes ‘Datos Correctos’ o en su defecto ‘Datos incorrectos’. Este es el código de delphi

Código Delphi [-]
function URLEncode(Str: string): string;
var
  i: integer;
begin
  Result:= '';
  for i:= 1 to Length(Str) do
    if Str[i] in ['A'..'Z','a'..'z','0'..'9','-','_','.'] then
      Result:= Result + Str[ i ]
    else
      Result:= Result + '%' + IntToHex(Ord(Str[ i ]),2);
end;
function SendRequest(Server, Uri: string; Port: Word; Params: TStringList;
  Response: TStream): Boolean;
var
  hNet: HINTERNET;
  hCon: HINTERNET;
  hReq: HINTERNET;
  Context: DWORD;
  Buffer: array[0..10240] of Char;
  BytesRead: DWORD;
  i: integer;
  Str: String;
  Success: Boolean;
begin
  Context:= 0;
  Result := FALSE;
  hNet := InternetOpen('Agente', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  if (hNet <> nil) then
  begin
    hCon:= InternetConnect(hNet,PChar(Server),Port,nil,nil,
      INTERNET_SERVICE_HTTP,0,Context);
    if (hCon <> nil) then
    begin
      if Params <> nil then
        hReq:= HttpOpenRequest(hCon,'POST',PChar(Uri),nil,nil,nil,
          INTERNET_FLAG_RELOAD,Context)
      else
        hReq:= HttpOpenRequest(hCon,'GET',PChar(Uri),nil,nil,nil,
          INTERNET_FLAG_RELOAD,Context);
      if (hReq <> nil) then
      begin
        if Params <> nil then
        begin
          Str:= Emptystr;
          for i:= 0 to Params.Count - 1 do
            Str:= Str + '&' + URLEncode(Params.Names[i]) + '=' +
              URLEncode(Params.ValueFromIndex[i]);
          Delete(Str,1,1);
          Success:= HttpSendRequest(hReq,
           'Content-Type: application/x-www-form-urlencoded',Cardinal(-1),
            PChar(Str),Length(Str));
        end else
          Success:= HttpSendRequest(hReq,nil,0,nil,0);
        if Success and (Response <> nil) then
        try
          while (InternetReadFile(hReq,@Buffer,sizeof(Buffer),BytesRead)) do
          begin
            if (BytesRead = 0) then
            begin
              Result := TRUE;
              break;
            end;
            Response.Write(Buffer,BytesRead);
          end;
        except end;
        InternetCloseHandle(hReq);
      end;
      InternetCloseHandle(hCon);
    end;
    InternetCloseHandle(hNet);
  end;
end;


procedure TForm2.Button1Click(Sender: TObject);
var Campos: TStringlist;Respuesta: TStringStream;
  ReturnValue : THandle;
begin
    Respuesta:= TStringStream.Create('');
    Campos:= TStringList.Create;
    Campos.Values['User']:= txtUsuario.Text;
    SendRequest('MiWeb','/PruebaServidor.php',80,campos, Respuesta);


    if Pos('Datos Correctos',Respuesta.DataString) > 0 then ShowMessage(Respuesta.DataString + 'Ok')
    else ShowMessage(Respuesta.DataString + ' Error');

    Campos.Free;
    Respuesta.Free;
end;



Ahora bien, esta primera parte estaría funcionando bien, ya que se conecta con la pagina y me devuelve un string, pero siempre me devuelve ‘Datos incorrectos’, por lo que presumo no le está llegando bien el parámetro pasado. Mi código PHP es este.
Código PHP:
<?php

   $Usuario 
$_POST['User'];  
   
$Usuario stripslashes($Usuario);
    echo  
$Usuario;
    if (
$Usuario == 'Hugo'){
    
       echo 
"   Datos Correctos"
       Exit();     
     }
     else {
        echo  
"   Datos Incorrectos"
        Exit(); 
     }

 
?>
Espero que alguien me pueda orientar. Desde ya agradezco su tiempo y paciencia. Saludos. Hugo
Responder Con Cita