简体   繁体   English

Indy 10中的TIdHTTP

[英]TIdHTTP in Indy 10

I used to use Indy back in the Delphi 6 days, and I am playing with Indy 10 now. 我过去常常在德尔福使用Indy 6天,现在我正在玩Indy 10。 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. 请求完成后,http_result应包含指定URL中的HTML。 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. 然而,这似乎不起作用,所以我觉得我应该使用IOHandler属性或组件的OnWork事件 - 但是对我来说用法似乎并不明显,我找不到任何有用的例子与谷歌。 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?). 附加信息:本着更具体的精神,我想知道:1。我这样做是正确的(或者我错过了什么?)。 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. 3.我正在使用的编译器/ os / Indy组合中总有可能存在错误。 (Although it should be working). (虽然它应该工作)。

I should mention, I always get a popup "Connection Closed Gracefully". 我应该提一下,我总是得到一个弹出“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. 我尝试用TRY捕获它......最后,但它不起作用。 Probably because Indy is triggering the exception in the background after the Get method runs I suppose. 可能是因为Indy在Get方法运行后在后台触发异常,我猜想。

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. 最后,这是一个运行程序的截屏视图,以清除任何混淆: http//screencast.com/t/NDMzNTQ5我希望HTML填充第二个备忘录框。

i think you have the TIdHTTP. 我认为你有TIdHTTP。 HandleRedirects property set to false, if you get the error " HTTP/1.1 302 Found " you can try this HandleRedirects属性设置为false,如果您收到错误“ HTTP / 1.1 302 Found ”,您可以尝试这个

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. 您必须将属性HandleRedirects设置为true。

There's no need for a form, using GExperts components to code I got this: 不需要表单,使用GExperts组件编写代码我得到了这个:

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. 只需将其粘贴到您的单元中,它就应该是您所需要的。

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. Synapse是一个轻量级的TCPIP库。 The library is being actively maintained and the current version runs fine in Delphi 2009/2010. 该库正在积极维护,当前版本在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). 它不是基于组件的框架,因此很容易与其他线程技术一起使用(例如OmniThreadLibraryAsyncCalls )。

Iirc if the website redirects, you also need to override some handler (onredirect or so). Iirc如果网站重定向,你还需要覆盖一些处理程序(onredirect左右)。 But this was also the case in indy9 iirc. 但在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. 我的解决方案是使用Synapse,正如其中一张海报所建议的那样。 It works on windows/Linux/Mac OS with minimal modifications, and works fine in libraries/threads. 它适用于Windows / Linux / Mac OS,只需要很少的修改,并且可以在库/线程中正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM