Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2844
  • Last Modified:

Using IdHTTP1.Get with Delphi and Indy 9/10 on a specific web server returns exception

I've a problem receiving a favicon.ico from a specific web server using Delphi and Indy 9/10. Other servers do work fine. The problem is not with this web server, as wget command line utility gets the file correctly.

here is the output from wget:
c:\a>wget http://perforce.eigenbase.org:8080/favicon.ico
SYSTEM_WGETRC = c:/progra~1/wget/etc/wgetrc
syswgetrc = c:/progra~1/wget/etc/wgetrc
--2013-01-27 00:12:39--  http://perforce.eigenbase.org:8080/favicon.ico
Resolving perforce.eigenbase.org... 72.14.190.177
Connecting to perforce.eigenbase.org|72.14.190.177|:8080... connected.
HTTP request sent, awaiting response... 200 No headers, assuming HTTP/0.9
Length: unspecified
Saving to: `favicon.ico'

    [ <=>                                   ] 2.862       --.-K/s   in 0s

2013-01-27 00:12:40 (143 MB/s) - `favicon.ico' saved [2862]

Open in new window

Here is my Delphi Indy 9/10 example code. It generates a "Connection Closed Gracefully" Exception, and the result is an empty string.
procedure TForm1.Button1Click(Sender: TObject);
var s: string;
begin
  s := '';
  try
  	s := IdHTTP1.Get('http://perforce.eigenbase.org:8080/favicon.ico');
  except
     on E: Exception do
     begin
          {$IFDEF DEBUG}ShowMessage('get error:'+E.Message){$ENDIF};
     end;
  end;
  ShowMessage(IntToStr(Length(s)));
end;

Open in new window

If I try the same code with a different server, for example:

s := IdHTTP1.Get('http://www.google.com/favicon.ico');

everything works just fine.

Is there a workaround to get the http://perforce.eigenbase.org:8080/favicon.ico file using IdHTTP1.Get from the server?
0
Casady
Asked:
Casady
  • 2
1 Solution
 
Sinisa VukCommented:
Indy is sometimes a pain .... It produces exception but internally is all ok.
Try this way...
...
      s :='';
      try
          s := IdHTTP1.Get('http://perforce.eigenbase.org:8080/favicon.ico');
          IdHTTP1.Disconnect(False); 
          IdHTTP1.IOHandler.InputBuffer.Clear;
        except
           on E: EIdConnClosedGracefully do 
           begin
              s := IdHTTP1.IOHandler.InputBufferAsString;
              if Length(Trim(s))=0 then
              begin
                s := IdHTTP1.ResponseText;
              end;
              IdHTTP1.Disconnect(False);
              IdHTTP1.IOHandler.InputBuffer.Clear;
           end else 
           begin
             ....
           end;
        end;
        ShowMessage(s);
...

Open in new window

0
 
CasadyAuthor Commented:
Well, I have accepted the solution too soon. :/

On the first look it works ok, because I've compared the first 20 bytes of the data I receive with wget and the data in IdHTTP1.ResponseText.

But the wget file is 2862 bytes, the content of IdHTTP1.ResponseText is only 2316 bytes. :(
0
 
Sinisa VukCommented:
As I said ... it is a pain... Try to use something else which works fine... Other way to download file:
http://www.swissdelphicenter.ch/torry/showcode.php?id=412
http://delphi.about.com/od/internetintranet/a/get_file_net.htm
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now