Ver Mensaje Individual
  #5  
Antiguo 03-08-2006
Avatar de seoane
[seoane] seoane is offline
Miembro Premium
 
Registrado: feb 2004
Ubicación: A Coruña, España
Posts: 3.717
Reputación: 24
seoane Va por buen camino
Una manera sencilla de hacerlo es como explica el compañero dec en el truco 346, utilizando los componentes Indy. Si no quieres complicarte mucho es la que te recomiendo.

Si quieres hacerlo mas difícil puedes usar Wininet, seria algo así:
Código Delphi [-]
uses Wininet;

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 HacerPost(Servidor, Cgi: string; Puerto: Word; Campos: TStringList): Boolean;
var
  hNet: HINTERNET;
  hCon: HINTERNET;
  hReq: HINTERNET;
  Context: DWORD;
  Mem: TMemoryStream;
  Str: string;
  i: integer;
  Buffer: array[0..10240] of Char;
  BytesRead: DWORD;
begin
  Context:= 0;
  Result := FALSE;
  Str:= '';
  hNet := InternetOpen('Agente', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  if (hNet <> nil) then
  begin
    hCon:= InternetConnect(hNet,PChar(Servidor),Puerto,nil,nil,INTERNET_SERVICE_HTTP,0,Context);
    if (hCon <> nil) then
    begin
      hReq:= HttpOpenRequest(hCon,'POST',PChar(Cgi),nil,nil,nil,
        INTERNET_FLAG_RELOAD,Context);
      if (hReq <> nil) then
      begin
        for i:= 0 to Campos.Count - 1 do
        begin
          Str:= Str + '&' + urlencode(Campos.Names[i]) + '=' +
            urlencode(Campos.ValueFromIndex[i]);
        end;
        Delete(Str,1,1);
        Mem:= TMemoryStream.Create;
        try
          try
            if HttpSendRequest(hReq,nil,0,PChar(Str),Length(Str)) then
            begin
              while (InternetReadFile(hReq, @Buffer, sizeof(Buffer), BytesRead)) do
              begin
                if (BytesRead = 0) then
               begin
                  Result := TRUE;
                  break;
                end; 
                Mem.Write(Buffer,BytesRead);
              end;
              // La información resultante la tienes en el Stream, puedes guardarla
              // o utilizarla para lo que la necesites.
              Mem.SaveToFile('c:\1.html');
            end;
          except end;
        finally
          Mem.Free;
        end;
        InternetCloseHandle(hReq);
      end;
      InternetCloseHandle(hCon);
    end;
    InternetCloseHandle(hNet);
  end;
end;


var
  Campos: TStringList;
begin
  Campos:= TStringList.Create;
  try
    Campos.Values['Nombre1']:= 'Valor1';
    Campos.Values['Nombre2']:= '@#~¬='; // Un valor con caracteres especiales
    Campos.Values['Nombre3']:= 'Valor3';
    HacerPost('www.servidor.com','/cgi-bin/test.cgi',80,Campos);
  finally
    Campos.Free;
  end;
end;

Última edición por seoane fecha: 03-08-2006 a las 18:00:42.
Responder Con Cita