English 中文(简体)
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.

最佳回答

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).

问题回答

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;

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.

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

This question has lingered open for quite some time, so I am closing it out. My solution was to just use Synapse, as one of the posters suggested. It works on windows/Linux/Mac OS with minimal modifications, and works fine in libraries/threads.





相关问题
determining the character set to use

my delphi 2009 app has a basic translation system that uses GNUGetText. i had used some win API calls to prepare the fonts. i thought it was working correctly until recently when someone from Malta ...

Help with strange Delphi 5 IDE problems

Ok, I m going nuts here. For the last (almost) four years, I ve been putting up with some extremely bad behavior from my Delphi 5 IDE. Problems include: Seemingly random errors in coride50.bpl ...

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How convert string to integer in Oxygene

In Delphi, there is a function StrToInt() that converts a string to an integer value; there is also IntToStr(), which does the reverse. These functions doesn t appear to be part of Oxygene, and I can ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签