Something odd is happening with the tidhttp component and I was wondering if
I can get some help here. I have a form with a button and a tidhttp
component. The onclick event is coded as bellow:
procedure TForm1.Button2Click(Sender: TObject);
var
vHtml : string;
begin
IdHTTP1.ProxyParams.BasicAuthentication := False;
IdHTTP1.Request.ContentLength := -1;
IdHTTP1.Request.Accept := 'text/html, */*';
IdHTTP1.Request.BasicAuthentication := true;
IdHTTP1.Request.UserAgent := 'Mozilla/3.0 (compatible; Indy Library)';
IdHTTP1.HTTPOptions := [hoForceEncodeParams];
IdHTTP1.Connect('http://www.google.com');
try
vHtml := IdHTTP1.Get('http://www.google.com');
finally
IdHTTP1.Disconnect(False);
end;
end;
When I click in button for the first time everything works fine, but when I
do a second click on the button I get the error:
---------------------------
Debugger Exception Notification
---------------------------
Project HttpConnIndy.exe raised exception class EIdSocketError with message
'Socket Error # 11004
'. Process stopped. Use Step or Run to continue.
---------------------------
OK Help
---------------------------
What's going on? If I close my project and run it again the error does not
happen. I guess that the disconnect method does not really disconnect the
socket. If I comment the line:
"IdHTTP1.Connect('http://www.google.com');"
everything works fine. I have debugged the source code of indy and I have
discovered that the method IdWinsock2.GetHostByName(PChar(AHostName)) in
IdStackWindows unit, at the second button click, dos not return an PHostEnt
(its nil).
I'm using indy 10.0.52
> IdHTTP1.Request.ContentLength := -1;
<snip>
> IdHTTP1.Connect('http://www.google.com');
<snip>
> IdHTTP1.Disconnect(False);
Get rid of all of that. -1 is not a valid ContentLength value, and you
should not be calling Connect() manually. Get() does that internally for
you. Try this code instead:
procedure TForm1.Button2Click(Sender: TObject);
var
vHtml : string;
begin
IdHTTP1.ProxyParams.BasicAuthentication := False;
IdHTTP1.Request.Accept := 'text/html, */*';
IdHTTP1.Request.BasicAuthentication := true;
IdHTTP1.Request.UserAgent := 'Mozilla/3.0 (compatible; Indy
Library)';
IdHTTP1.HTTPOptions := [hoForceEncodeParams];
vHtml := IdHTTP1.Get('http://www.google.com');
end;
> I guess that the disconnect method does not really disconnect the socket.
Yes, it does.
> If I comment the line:
>
> "IdHTTP1.Connect('http://www.google.com');"
>
> everything works fine.
You should not have been calling Connect() to begin with.
> I have debugged the source code of indy and I have discovered
> that the method IdWinsock2.GetHostByName(PChar(AHostName)) in
> IdStackWindows unit, at the second button click, dos not return an
> PHostEnt (its nil).
Which means the OS itself, not Indy, is failing to resolve the specified
hostname. Which makes sense because 'http://www.google.com' is not a valid
hostname when establishing a socket connection. It is a URL that contains a
hostname which TIdHTTP extracts before calling Connect() internally. If you
are going to call Connect() manually in any componnet, do not specify
protocol information at all, ie:
Connect('www.google.com');
> I'm using indy 10.0.52
That is a VERY old version. You should consider upgrading.
Gambit
OK .. thanks! I will not call connect at all. How can I do to get the newest
indy version? I just found svn snapshots, and I'm not interested to get an
developing version.
Rafael
"Remy Lebeau (TeamB)" <no....@no.spam.com> escreveu na mensagem
news:484966a1$3...@newsgroups.borland.com...
Thanks for all
Rafael
"Rafael" <tecno...@prolink.inf.br> escreveu na mensagem
news:4849...@newsgroups.borland.com...
> How can I do to get the newest indy version? I just found svn snapshots
That is the latest version.
> I'm not interested to get an developing version.
Snapshots are the only versions available.
Gambit