views:

841

answers:

4
+2  Q: 

TIdHTTP in Indy 10

I used to use Indy back in the Delphi 6 days, and I am playing with Indy 10 now. What I want to do is incredibly simple, but I don't see a simple way of doing it, so I must be missing something.

What I want to do is something like this:

Here is the actual code I am using:

procedure TForm1.btnGetURLClick(Sender: TObject);
begin
  moHeader.Lines.Clear;
  moBody.Lines.Clear;
  try
    moBody.text := IdHttp1.Get(edURL.text);
  finally
  end;
end;

When the request is complete, the http_result should contain the HTML from the URL specified. This doesn't seem to work however, so I get the feeling I should perhaps be using the IOHandler property or the OnWork event of the component - however the usage doesn't seem obvious to me, and I couldn't find any working examples with google. I am sure this is something that has been done before, so any help would be appreciated.

Additional Information: In the spirit of being more specific, I want to know: 1. Am I doing this right to begin with (or did I miss something?). 2. If so, why might it not be working. 3. It is always possible that there is a bug in the combination of compiler/os/Indy I am using. (Although it should be working).

I should mention, I always get a popup "Connection Closed Gracefully". This seems to be an exception, and it could be interfering with the result of the function. I attempted to trap this with a TRY...FINALLY, but it doesn't work. Probably because Indy is triggering the exception in the background after the Get method runs I suppose.

Finally, here is a screencast of the program running to clear up any confusion: http://screencast.com/t/NDMzNTQ5 I expect the HTML to fill the second memo box.

A: 

Iirc if the website redirects, you also need to override some handler (onredirect or so). But this was also the case in indy9 iirc.

Marco van de Voort
I plan on using this with a local daemon, so redirects won't be an issue, but it could be that they are at the moment, since I have been testing with www.google.com, etc. On the other hand, I can't get *any* site to work so far, which lead me to believe I must be doing it the wrong way.
Noah
TIdHTTP handles redirects automatically by default. Look at the HandleRedirects and RedirectMaximum properties.
Remy Lebeau - TeamB
+2  A: 

You have to set the property HandleRedirects to true.

There's no need for a form, using GExperts components to code I got this:

var
  IdHTTP: TIdHTTP;

IdHTTP := TIdHTTP.Create(Self);
with IdHTTP do
begin
  Name := 'IdHTTP';
  AllowCookies := True;
  HandleRedirects := True;
  HTTPOptions := [hoForceEncodeParams];
end;

Just paste this in your unit, it should be all you need.

johnny
+4  A: 

i think you have the TIdHTTP.HandleRedirects property set to false, if you get the error "HTTP/1.1 302 Found" you can try this

var
http_result:string;    
Begin
IdHTTP1.HandleRedirects:=True;
http_result := IdHTTP1.Get('http://www.google.com');

End;
RRUZ
Hi, this actually was happening. I turned the HandleRedirects property to true now. I still get the: HTTP/1.1 302 Found, though.
Noah
A 302 is a redirect. TIdHTTP handles it just fine, as long as the server is providing a "Location" header with a new URL.
Remy Lebeau - TeamB
+2  A: 

Another option, would be to use synapse. This is all that is needed to retrieve a webpage using this library:

uses
  ...,HTTPSEND;

var
  Result : TStrings;


  if HTTPGetText('http://www.google.com',Result) then
    // do something with result

Synapse is a lightweight TCPIP library. The library is being actively maintained and the current version runs fine in Delphi 2009/2010. It is NOT a component based framework, so it is very easy to use with other threading techniques (OmniThreadLibrary or AsyncCalls for example).

skamradt
I will certainly take a look at it. I went with Indy because I was familiar with it from using it years ago in Delphi 6 or so.
Noah
I got Synapse working with a little work (Supports FPK out of the box, but not on MacOS/BSD...). This is a good option for my use I think, but I will leave the question open a little longer, since having it answered by fixing Indy may help other people who search for such an answer later.
Noah