Dismiss Notice

REGISTRATION IS AFTER ADMIN ACTIVATION

DONATIONS WITH PAYPAL CLICK TO BUTTON

1 MONTH VIP - 5$; 3 MONTHS VIP - 10$; 6 MONTHS VIP - 20$; 1 YEAR VIP - 30$; 2 YEARS VIP - 50$; GOLD USER FOR LIFE VIP - 150$

DONATIONS WITH Bitcoin Address:3NRs3CK3fhXifrNYxHEZKpETDd9vNLMsMD

Dismiss Notice
The registration is approved by the Administrator. It takes about 1 day to approve your registration
Dismiss Notice
For open hidden message no need write thanks, thank etc. Enough is click to like button on right side of thread.

Using Indy idHTTP to post binary and text

Discussion in 'Delphi Programming' started by AdminDF, Feb 1, 2014.

  1. AdminDF
    Online

    AdminDFAdminDF is a Verified Member DelphiFan Administrator Staff Member DF Staff

    [font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]Problem/Question/Abstract:[/font]

    [font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]Using Indy idHTTP to post binary and text[/font]

    [font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]Answer:[/font]

    [font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]This is a small example of using post to send data to web server. There is two different ways to do this operation.[/font]

    [font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]Solve 1:[/font]


    Code:
    procedure TForm1.SendPostData;
    const
    CRLF = #13#10;
    var
    aStream: TMemoryStream;
    Params: TMemoryStream;
    S: string;
    begin
    aStream := TMemoryStream.create;
    Params := TMemoryStream.Create;
    
    HTTP.Request.ContentType := 'multipart/form-data;
    boundary = - - - - - - - - - - - - - - - - - - - - - - - - - - - - -7
    cf87224d2020a';
    
    try
    S := '-----------------------------7cf87224d2020a' + CRLF +
    'Content-Disposition: form-data; name="file1"; filename="c:abc.txt"' +
    CRLF +
    'Content-Type: text/plain' + CRLF + CRLF +
    'file one content. Contant-Type can be application/octet-stream or if
    you want you can ask your OS fot the exact type
    .' + CRLF +
    '-----------------------------7cf87224d2020a' + CRLF +
    'Content-Disposition: form-data; name="sys_return_url2"' + CRLF + CRLF +
    'hello2' + CRLF +
    '-----------------------------7cf87224d2020a--';
    
    Params.Write(S[1], Length(S));
    
    with HTTP do
    begin
    try
    HTTP.Post('http://www.mydomain.com/postexampe.cgi', Params,
    aStream);
    except
    on E: Exception do
    showmessage('Error encountered during POST: ' + E.Message);
    end;
    end;
    aStream.WriteBuffer(#0' ', 1);
    showmessage(PChar(aStream.Memory));
    except
    end;
    end;
    
    
    Solve 2:
    
    procedure TForm1.SendPostData;
    var
    aStream: TMemoryStream;
    Params: TStringStream;
    begin
    aStream := TMemoryStream.create;
    Params := TStringStream.create('');
    HTTP.Request.ContentType := 'application/x-www-form-urlencoded';
    
    try
    Params.WriteString(URLEncode('sys_return_url=' + 'helo1' + '&'));
    Params.WriteString(URLEncode('sys_return_url=' + 'helo2'));
    with HTTP do
    begin
    try
    HTTP.Post('http://www.mydomain.com/postexampe.cgi', Params,
    aStream);
    except
    on E: Exception do
    showmessage('Error encountered during POST: ' + E.Message);
    end;
    end;
    aStream.WriteBuffer(#0' ', 1);
    showmessage(PChar(aStream.Memory));
    except
    end;
    end;



    [font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]As you can see there is a difference in the way post stream is constructed and the ContentType. In the first example ContentType is "multipart/form-data; boundary=-----------------------------7cf87224d2020a" and this boundary is used to separate different parameters.[/font]

    [font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]In the second example the ContentType is "application/x-www-form-urlencoded". In this case the paremeteras are passed in the form[/font]

    [font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]ParamName=ParamValue&ParamName=ParamValue[/font]

    [font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]Note that the Pramaeters in the second form must be URL encoded.[/font]

    [font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]Where these two formats of post information are used?[/font]

    [font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]The first one is used when you have binary data to post and the second one is when you are going to post only text fields.[/font]
     

Share This Page

Laws
Our website, law, laws, copyright and aims to be respectful of individual rights. Our site, as defined in the law of 5651 serves as a provider of space. According to the law, contrary to the law of site management has no obligation to monitor content. Therefore, our site has adopted the principle of fit and remove. Works are subject to copyright laws and is shared in a manner that violated their legal rights, or professional associations, rights holders who, adsdelphi@gmail.com I can reach us at e-mail address. Complaints considered to be infringing on the examination of the content will be removed from our site.
Donate Help To Us and Be VIP
DONATIONS WITH PAYPAL CLICK TO BUTTON
6 MONTHS VIP - 20$; 1 YEAR VIP - 30$; 2 YEARS VIP - 50$; GOLD USER FOR LIFE VIP - 150$
Social Block