| View previous topic :: View next topic |
| Author |
Message |
dESFasado Guest
|
Posted: Mon Oct 27, 2003 12:19 pm Post subject: TidHTTPClient |
|
|
Hi there.
I'm having some troubles with TIdHTTPClient doing POST. It seems that Indy append one more & ampersand than requiered. For example 2 params then 2 &.
Which is the best/easy way to POST using TiDHTTPClient (List, stream, etc)
If its my mistake here, please give me some lines that actually post.
TIA.
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon Oct 27, 2003 6:43 pm Post subject: Re: TidHTTPClient |
|
|
"dESFasado" <ask (AT) ciudad (DOT) com.ar> wrote
| Quote: | I'm having some troubles with TIdHTTPClient doing POST.
|
Indy has no such component. Perhaps you are referring to TIdHTTP instead?
| Quote: | It seems that Indy append one more & ampersand than requiered.
|
Please show your actual code that is setting up and calling Post().
Gambit
|
|
| Back to top |
|
 |
dESFasado Guest
|
Posted: Mon Oct 27, 2003 8:27 pm Post subject: Re: TidHTTPClient |
|
|
En Mon, 27 Oct 2003 10:43:46 -0800, Remy Lebeau (TeamB) <gambit47.no.spam (AT) no (DOT) spam.yahoo.com> escribió:
Here is the code I'm trying to work with POST.
I know that I must encode strings with special characters (space, @, etc) but the problem isn't related to that. Well I think it doesn't to.
It has no referer, but thats ok for the purporse... Anyway I think the problem is another.
protocol in object properties is 1.1
procedure TForm1.Button1Click(Sender: TObject);
var
my_param_list:tstringlist;
begin
my_param_list:=tstringlist.Create;
my_param_list.Add('to=xxxx');
my_param_list.Add('from=yyyy');
my_param_list.Add('type=zzzz');
self.IdHTTP1.HandleRedirects:=true;
self.IdHTTP1.CookieManager:=CookieMan;
self.IdHTTP1.Request.Accept := 'text/plain';
self.IdHTTP1.Host:='people.icq.com';
self.IdHTTP1.Port:=80;
self.IdHTTP1.Request.ContentType:='application/x-www-form-urlencoded';
//
// *** WHAT TO DO HERE? ***
// I'VE DONE MANY CALCULATIONS AND NONE WORKED OUT
//
self.IdHTTP1.Request.ContentLength:=
self.IdHTTP1.Request.UserAgent:='Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
self.IdHTTP1.Request.Connection:='Connection: Keep-Alive';
self.IdHTTP1.Connect();
self.IdHTTP1.Post('/cgi-bin/cgi_programa.cgi', my_param_list );
self.IdHTTP1.Disconnect;
my_param_list.free;
end;
Any ideas?
Thanks in advance.
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon Oct 27, 2003 8:50 pm Post subject: Re: TidHTTPClient |
|
|
"dESFasado" <ask (AT) ciudad (DOT) com.ar> wrote
| Quote: | my_param_list:tstringlist;
|
Try using a TIdMutlPartDataStream instead of a TStringList.
| Quote: | self.IdHTTP1.Host:='people.icq.com';
|
You should not be setting the Host yourself. You shuld be putting the Host
into the actual URL that you pass to Get/Post():
self.IdHTTP1.Post('http://people.icq.com/cgi-bin/cgi_programa.cgi',
my_param_list );
| Quote: | self.IdHTTP1.Connect();
|
You should not be calling Connect() and Disconnect() yourself. Get/Post()
handle that internally.
| Quote: | my_param_list.free;
|
You should be wrapping your code into a try...except or try...finally block
so that you can free the list correctly in case TIdHTTP throws an exception
when an error occurs.
Gambit
|
|
| Back to top |
|
 |
eshipman Guest
|
Posted: Mon Oct 27, 2003 8:50 pm Post subject: Re: TidHTTPClient |
|
|
<SNIP>
| Quote: |
//
// *** WHAT TO DO HERE? ***
// I'VE DONE MANY CALCULATIONS AND NONE WORKED OUT
//
SNIP |
Well, I have never had to calculate it... Here's and example:
procedure TForm1.SendPostData;
Var
aStream: TMemoryStream;
Params: TStringStream;
begin
aStream := TMemoryStream.create;
Params := TStringStream.create('');
try
with IdHTTP1 do
begin
Params.WriteString(URLEncode('teste=' + 'yes' + '&'));
Params.WriteString(URLEncode('name=' + 'ivan' + '&'));
Params.WriteString(URLEncode('number=' + '102'));
Request.ContentType := 'application/x-www-form-urlencoded';
try
Post('http://localhost/teste.asp', Params, aStream);
except
on E: Exception do
showmessage('Error encountered during POST: ' + E.Message);
end;
end;
aStream.WriteBuffer(#0' ', 1);
aStream.Position := 0;
Memo1.Lines.LoadFromStream(aStream);
except
end;
end;
|
|
| Back to top |
|
 |
dESFasado Guest
|
Posted: Mon Oct 27, 2003 9:46 pm Post subject: Re: TidHTTPClient |
|
|
En Mon, 27 Oct 2003 12:50:15 -0800, Remy Lebeau (TeamB) <gambit47.no.spam (AT) no (DOT) spam.yahoo.com> escribió:
First of all. THANKS VERY MUCH. read on if you have time for newbie questions.
| Quote: |
"dESFasado" <ask (AT) ciudad (DOT) com.ar> wrote in message
news:oprxpxshw0mhmcrp (AT) localhost (DOT) ..
my_param_list:tstringlist;
Try using a TIdMutlPartDataStream instead of a TStringList.
|
Why? the overloaded method say so. Is there any working difference in the end result?
| Quote: |
self.IdHTTP1.Host:='people.icq.com';
You should not be setting the Host yourself. You shuld be putting the Host
into the actual URL that you pass to Get/Post():
self.IdHTTP1.Post('http://people.icq.com/cgi-bin/cgi_programa.cgi',
my_param_list );
|
I suppose this is for safe-coding..
| Quote: | self.IdHTTP1.Connect();
You should not be calling Connect() and Disconnect() yourself. Get/Post()
handle that internally.
|
Thanks for this one. make much clear code;
| Quote: |
my_param_list.free;
You should be wrapping your code into a try...except or try...finally block
so that you can free the list correctly in case TIdHTTP throws an exception
when an error occurs.
|
I know, sorry it was just-in-time code snippet.
Well THANKS VERY MUCH
Indy is sometimes a little trickier for me since documentation (CHTML ver) is a little messy. For example exceptions has none relationship at all with what method raise them.
Anyway THANKS AGAIN
cyas.
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon Oct 27, 2003 10:22 pm Post subject: Re: TidHTTPClient |
|
|
"dESFasado" <ask (AT) ciudad (DOT) com.ar> wrote
Because it is supposed to handle posting better than TStringList does in
regards to simulating HTML form submissions, such as you are currently
attempting to do.
| Quote: | Indy is sometimes a little trickier for me since documentation (CHTML ver)
is a little messy. For example exceptions has none relationship at all
with
what method raise them.
|
Sometimes they do. But you have to keep in mind that Indy is built with
functionality layered on top of functionality on top of more functionality,
so sometimes the exceptions are coming from much lower levels of the library
then the top-level components themselves. You should pretty much wrap all
Indy operations in exception handling just to make sure you catch
everything.
Gambit
|
|
| Back to top |
|
 |
dESFasado Guest
|
Posted: Tue Oct 28, 2003 3:36 am Post subject: Re: TidHTTPClient |
|
|
En Mon, 27 Oct 2003 14:50:44 -0600, escribió:
Thanks for the code. Now I can really post. Thanks
A question about the code:
<SNIP>
| Quote: | aStream.WriteBuffer(#0' ', 1);
|
I'm not a delphi expert, so...
what the above line really does? I mean isn't it enough to set the position to 0?
Is this a null terminated stream or something similiar?
if yes, that a really handy way of writing the NULL, but a kind of tricky ;-)
| Quote: | aStream.Position := 0;
Memo1.Lines.LoadFromStream(aStream);
|
<SNIP>
Just Thanks again.
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Tue Oct 28, 2003 3:53 am Post subject: Re: TidHTTPClient |
|
|
"dESFasado" <ask (AT) ciudad (DOT) com.ar> wrote
| Quote: | what the above line really does?
|
From the looks of it, it is adding a null terminating character to the
stream without having to declare and assign a separate character variable
first.
Gambit
|
|
| Back to top |
|
 |
dESFasado Guest
|
Posted: Wed Oct 29, 2003 1:16 pm Post subject: Re: TidHTTPClient |
|
|
En Mon, 27 Oct 2003 12:50:15 -0800, Remy Lebeau (TeamB) <gambit47.no.spam (AT) no (DOT) spam.yahoo.com> escribió:
| Quote: |
Try using a TIdMutlPartDataStream instead of a TStringList.
|
But they are 2 different types of encodings. Isn't it up to the server decide which type of encodings he likes for POST?
Bye
|
|
| Back to top |
|
 |
|