2012. június 21., csütörtök

Using Indy idHTTP to post binary and text


Problem/Question/Abstract:

Using Indy idHTTP to post binary and text

Answer:

This is a small example of using post to send data to web server. There is two different ways to do this operation.

Solve 1:

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;

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.

In the second example the ContentType is "application/x-www-form-urlencoded". In this case the paremeteras are passed in the form

ParamName=ParamValue&ParamName=ParamValue

Note that the Pramaeters in the second form must be URL encoded.

Where these two formats of post information are used?

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.

4 megjegyzés:

  1. I came on the Internet i saw great testimony about DR Voke on how he was able to cure someone from HERPES, and any disease this person said great things about this man, and advice we contact him for any problem that DR Voke can be of help, well i decided to give him a try, he requested for my information which i sent to him, and he told me he was going to prepare for me a healing portion, which he wanted me to take for days, and after which i should go back to the hospital for check up, well after taking all the treatment sent to me by DR Voke i went back to the Hospital for check up, and now i have been confirmed HERPES Negative, friends you can reach Dr voke on any treatment for any Disease, reach him on _________________________________[doctorvoke@yahoo.com]

    VálaszTörlés
  2. i want to inform the public how i was cured of herpes  Virus by a Doctor called Dr ishiaku. I visited different hospitals but they gave me a list of drugs like Famvir, Zovirax, and Valtrex which are very expensive to treat the symptoms and never cured me. I was browsing through the Internet searching for remedies on Herpes and I saw comments of people talking about how Doctor ishiaku cured them. I Was scared because i never believed in the Internet but i was convinced to give him a try because i was having no hope of been cured of herpes so i decided to contact him on his email, i searched his email on net and i saw a lot of people testifying about his goodness. When I contacted him he gave me hope and send a Herbal medicine to me that i took and it seriously worked for me, am a free person now without problem, my HERPES result came out negative. You can contact him on his email: ishiakuherbalcure@gmail.com or whats-app him via + 2348180828544 and get all your problems solved.
    1)Sickness of any kind
    2)Help to CURE Hepatitis B, herpes, HIV/AIDS CANCER and BIPOLAR
    3)Divorce, Breakup problem and To Re-unit
    4)Pregnancy problem
    5)Financial problem and Job promotion
    6)bad mouth odour
    7)eye or ear problem
    8) enlargement of manhood
    you can also visit his website for information
    www.ishiakuherbalcure.wordpress.com  

    VálaszTörlés
    Válaszok
    1. I'm 61 years old. I contracted hpv in 2011' I has be taking lot treatment for it and embarrassed some months ago the wart stated coming out seriously, I used lot recommendation because there was lot warts around my anus and was so . but today I'm totally happy I got the virus eliminated by using natural treatment from Dr voodoo herbal center after his treatment I got cured. all the warts went away' seriously believed Dr. voodoo he have the cure for human papillomavirus because he has eliminated hpv been in my body since 2011, Dr. voodoo make it possible for me. Here is Dr. voodoo email: voodoospelltemple66@gmail.com or WhatsApp him via +2348140120719 he is welled capable of curing terrible diseases.

      Törlés
  3. I visited different hospital but they gave me list of expensive drugs to treat the symptoms and never cured me. A closed friend of mine introduce me to a Herbal Doctor who cured her. I was scared to contact him because of what i experienced from fake doctors. she encouraged me and gave me his whatsapp contact,and i contacted him and i asked him series of questions, i also ask him to show me the results of those he have cured with his herbal drugs,and he gave me all the information i needed that gave me hope. He send a Herbal medicine to me that i took and it seriously worked for me. am now Hepatitis negative. God bless you for being a sincere and great man. Am so excited and am free from herpes virus. you can reach him via email;drshaibuabacha@gmail.com or whats-app/call on +2349051727475. He also cure several ailments like ; Mouth odour,  Diabetes, Stroke, Hypertension,  kidney problem, Ex back, Low sperm count, Weak Erection and HIV/Aids. Please do not hide you problems so your problems will not hide you.

    VálaszTörlés