Ver Mensaje Individual
  #1  
Antiguo 01-11-2006
Avatar de dec
dec dec is offline
Moderador
 
Registrado: dic 2004
Ubicación: Alcobendas, Madrid, España
Posts: 13.107
Reputación: 34
dec Tiene un aura espectaculardec Tiene un aura espectacular
HTTP GET y HTTP POST utilizando WinInet

Bueno, como alternativa a Indy podemos usar Wininet. Podemos hacer Get, podemos hacer Post y maneja las cookies perfectamente incluso las de sesión. No tengo nada en contra de las Indy, es solo por dar alternativas, además no se pueden usar en el Turbo :( , aunque eso no es culpa suya.

Bueno, un poco de código para animar la cosa:
Código Delphi [-]
uses Windows, SysUtils, Classes,Wininet;

// URL Encode y Decode para codificar los strings segun la norma RFC 1738
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 URLDecode(Str: string): string;
var
  i: integer;
begin
  Result:= '';
  Str:= StringReplace(Str, '+', ' ', [rfReplaceAll]);
  while Length(Str) > 0 do
  begin
    if Copy(Str, 1, 1) = '%' then
    begin
      if not TryStrToInt('$' + Copy(Str, 2, 2),i) then
      begin
        Result:= '';
        Exit;
      end;
      Result:= Result + Char(i);
      Delete(Str, 1, 2);
    end else Result:= Result + Copy(Str, 1, 1);
    Delete(Str,1,1);
  end;
end;

// Con esta funcion hacemos Get y nos devuleve el resultado en un stream
function Get(Url: string; Stream: TStream): Boolean;
var
  hNet: HINTERNET;
  hUrl: HINTERNET;
  Buffer: array[0..10240] of Char;
  BytesRead: Cardinal;
begin
  Result:= FALSE;
  hNet:= InternetOpen('Agente', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  if (hNet <> nil) then
  begin
    hUrl:= InternetOpenUrl(hNet, PChar(Url), nil, 0,
      INTERNET_FLAG_RELOAD, 0);
    if (hUrl <> nil) then
    begin
      while (InternetReadFile(hUrl, @Buffer, sizeof(Buffer), BytesRead)) do
      begin
        if (BytesRead = 0) then
        begin
          Result:= TRUE;
          break;
        end;
        Stream.Write(Buffer,BytesRead);
      end;
      InternetCloseHandle(hUrl);
    end;
    InternetCloseHandle(hNet);
  end;
end;

// Con esta funcion hacemos Post, los campos del formulario se pasan en PostString
// como pares nombre=valor
function Post(Servidor, Pagina: string; Puerto: Word;
  PostStrings: TStringList; Stream: TStream): Boolean;
var
  hNet: HINTERNET;
  hCon: HINTERNET;
  hReq: HINTERNET;
  Context: DWORD;
  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(Pagina),nil,nil,nil,
        INTERNET_FLAG_RELOAD,Context);
      if (hReq <> nil) then
      begin
        for i:= 0 to PostStrings.Count - 1 do
        begin
          Str:= Str + '&' + URLEncode(PostStrings.Names[i]) + '=' +
            URLEncode(PostStrings.ValueFromIndex[i]);
        end;
        Delete(Str,1,1);
        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;
              Stream.Write(Buffer,BytesRead);
            end;
          end;
          except end;
        InternetCloseHandle(hReq);
      end;
      InternetCloseHandle(hCon);
    end;
    InternetCloseHandle(hNet);
  end;
end;

Un ejemplo de como usar lo anterior.
Código Delphi [-]
var
  Campos: TStringlist;
  Stream: TMemoryStream;
begin
  Campos:= TStringList.Create;
  Stream:= TMemoryStream.Create;
  try
    Campos.Values['Nombre']:= 'Valor';
    Post('www.clubdelphi.com','/',80,Campos,Stream);
    Stream.SaveToFile('d:\1.txt');
    Stream.Clear;
    Get('http://www.clubdelphi.com/',Stream);
    Stream.SaveToFile('d:\2.txt');
  finally
    Campos.Free;
    Stream.Free;
  end;
end;
Responder Con Cita