2008-11-19 30 views

Respuesta

43

Usando Indy. Coloque sus parámetros en StringList (name = value) y simplemente llame a Post con la URL y StringList.

function PostExample: string; 
var 
    lHTTP: TIdHTTP; 
    lParamList: TStringList; 
begin 
    lParamList := TStringList.Create; 
    lParamList.Add('id=1'); 

    lHTTP := TIdHTTP.Create; 
    try 
    Result := lHTTP.Post('http://blahblahblah...', lParamList); 
    finally 
    lHTTP.Free; 
    lParamList.Free; 
    end; 
end; 
+0

TIdHTTP support also https? – Ampere

+1

Sí, TIdHTTP es compatible con HTTPS. http://stackoverflow.com/a/6693653/19183 –

+0

Son los parámetros de 'lParamList' los mismos que los params sin analizar de una solicitud para delphi del servidor http (estoy intentando enviar una solicitud posterior al servidor http delphi ya creado) –

8

De nuevo, Synapse TCP/IP library al rescate. Use la rutina HTTPSEND HTTPPostURL.

function HttpPostURL(const URL, URLData: string; const Data: TStream): Boolean; 

La URL sería el recurso a publicar también, el urldata sería los datos del formulario, y sus resultados XML volvería como un flujo de datos.

+1

Synapse es definitivamente otra opción viable. –

14

He aquí un ejemplo del uso de Indy en dejar un JPEG a un servidor web corriendo Gallery

Tengo más ejemplos de este tipo de cosas (yo los uso en un protector de pantalla he escrito en Delphi para el proyecto Galería disponibles here, o más información en el sitio web de la Galería here).

El bit importante que supongo es que el JPEG pasa como una secuencia.

procedure AddImage(const AlbumID: Integer; const Image: TStream; const ImageFilename, Caption, Description, Summary: String); 
var 
    Response: String; 
    HTTPClient: TidHTTP; 
    ImageStream: TIdMultipartFormDataStream; 
begin 

    HTTPClient := TidHTTP.Create; 

    try 
    ImageStream := TIdMultiPartFormDataStream.Create; 
    try 
     ImageStream.AddFormField('g2_form[cmd]', 'add-item'); 
     ImageStream.AddFormField('g2_form[set_albumId]', Format('%d', [AlbumID])); 
     ImageStream.AddFormField('g2_form[caption]', Caption); 
     ImageStream.AddFormField('g2_form[force_filename]', ImageFilename); 
     ImageStream.AddFormField('g2_form[extrafield.Summary]', Summary); 
     ImageStream.AddFormField('g2_form[extrafield.Description]', Description); 

     ImageStream.AddObject('g2_userfile', 'image/jpeg', Image, ImageFilename); 

     Response := HTTPClient.Post('http://mygallery.com/main.php?g2_controller=remote:GalleryRemote', ImageStream); 
    finally 
     ImageStream.Free; 
    end; 
    finally 
    HTTPClient.Free; 
    end; 
end; 
Cuestiones relacionadas