Delphi 2009
我最近从多个GET请求切换到单个POST,我认为这会更高效,但事实证明它要慢得多。它从1-2秒变为8-10秒,我无法弄清楚原因。
例如
unit Unit4;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdHTTP;
type
TForm4 = class(TForm)
d: TIdHTTP;
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form4: TForm4;
implementation
{$R *.dfm}
procedure TForm4.Button1Click(Sender: TObject);
var m: tmemorystream;
data: tstringlist;
i: integer;
begin
memo1.Clear;
m:=tmemorystream.Create;
data:=tstringlist.Create;
data.Add(format('test1');
data.Add(format('test2');
d.Post('http://www.someurl.com/test.php', data, m);
m.Position:=0;
data.LoadFromStream(m);
memo1.Lines.Add('Received:');
for i := 0 to data.count - 1 do
memo1.Lines.Add(data[i]);
m.Free;
data.Free;
end;
end.
并在服务器上
<?php
echo "1\n";
?>
答案 0 :(得分:6)
确保hoKeepOriginalProtocol
属性中已启用TIdHTTP.HTTPOptions
标记。默认情况下,TIdHTTP.Post()
强制每次调用连接时都关闭,无论使用HTTP keep-alives,除非启用该标志。断开连接和重新连接的额外开销可能会占用额外的时间。
答案 1 :(得分:5)
我使用不同的网址进行了简单的测试:
d.Post('http://whatismyip.org', data, m); // Using DNS name
或
d.Post('http://54.242.203.46', data, m); // Using IP
The Post花了大约1秒钟,所以我认为你的问题可能与Indy无关。我的建议:
这些步骤可以缩小问题所在。
如果这些都没有帮助 - 请尝试将Indy升级到最新版本。