IdHttp只需获取响应代码 [英] IdHttp Just Get Response Code
问题描述
我已经查找了idhttp帮助文档,唯一的方法可以看到可能做到这一点是将代码分配给一个 MemoryStream
然后立即清除它,但是这不是很有效,并且使用不需要的内存。有没有办法只是调用一个网站并获得响应,但是忽略发回的HTML效率更高,不会浪费内存?
目前,代码看起来像这样。但是这只是我还没有测试的示例代码,我只是用它来解释我正在做什么。
过程Button1Click(Sender:TObject);
var
http:TIdHttp;
s:TStream;
url:string;
code:integer;
begin
s:= TStream.Create();
http:= Tidhttp.create();
url:='http://www.WEBSITE.com';
try
http.get(url,s);
code:= http.ResponseCode;
ShowMessage(IntToStr(code));
finally
s.Free();
http.Free();
end;
TIdHTTP.Head()
是最好的选择。但是,作为一种替代方案,在最新版本中,您可以调用 TIdHTTP.Get()
,其中包含一个零目标 TStream
或$ code> TIdEventStream ,没有分配任何事件处理程序,它仍将读取服务器的数据,但不存储在任何地方。
无论哪种方式,还要记住,如果服务器发回ResponseCode故障,TIdHTTP将引发异常(除非您使用 AIgnoreReplies
参数来指定特定的ResponseCode值有兴趣的话,所以你应该说明一下,例如:
procedure Button1Click(Sender:TObject);
var
http:TIdHttp;
url:string;
code:integer;
begin
url:='http://www.WEBSITE.com';
http:= TIdHTTP.Create(nil);
尝试
尝试
http.Head(url);
code:= http.ResponseCode;
除了
在E:EIdHTTPProtocolException do
code:= http.ResponseCode; //或:code:= E.ErrorCode;
结束
ShowMessage(IntToStr(code));
finally
http.Free();
结束
结束
程序Button2Click(Sender:TObject);
var
http:TIdHttp;
url:string;
code:integer;
begin
url:='http://www.WEBSITE.com';
http:= TIdHTTP.Create(nil);
尝试
尝试
http.Get(url,nil);
code:= http.ResponseCode;
除了
在E:EIdHTTPProtocolException do
code:= http.ResponseCode; //或:code:= E.ErrorCode;
结束
ShowMessage(IntToStr(code));
finally
http.Free();
结束
结束
I'm using idhttp (Indy) to do some website checking. All I want it to do is check the response code from the server after my request has been sent, I don't want to actually have to receive the HTML output from the server as I'm only monitoring for a 200 OK code, any other code meaning there is an issue of some form.
I've looked up idhttp help documents and the only way I could see to possible do this would be to assign the code to a MemoryStream
and then just clear it straight away, however that isn't very efficient and uses memory that isn't needed. Is there a way to just call a site and get the response but ignore the HTML sent back that is more efficient and doesn't waste memory?
Currently the code would look something like this. However this is just sample code which I haven't tested yet, I'm just using it to explain what I'm trying to do.
Procedure Button1Click(Sender: TObject);
var
http : TIdHttp;
s : TStream;
url : string;
code : integer;
begin
s := TStream.Create();
http := Tidhttp.create();
url := 'http://www.WEBSITE.com';
try
http.get(url,s);
code := http.ResponseCode;
ShowMessage(IntToStr(code));
finally
s.Free();
http.Free();
end;
TIdHTTP.Head()
is the best option. However, as an alternative, in the latest version you can call TIdHTTP.Get()
with a nil destination TStream
, or a TIdEventStream
with no event handlers assigned, and it will still read the server's data but not store it anywhere.
Either way, also keep in mind that if the server sends back a failure ResponseCode, TIdHTTP will raise an exception (unless you use the AIgnoreReplies
parameter to specify specific ResponseCode values you are interested in), so you should account for that as well, eg:
procedure Button1Click(Sender: TObject);
var
http : TIdHttp;
url : string;
code : integer;
begin
url := 'http://www.WEBSITE.com';
http := TIdHTTP.Create(nil);
try
try
http.Head(url);
code := http.ResponseCode;
except
on E: EIdHTTPProtocolException do
code := http.ResponseCode; // or: code := E.ErrorCode;
end;
ShowMessage(IntToStr(code));
finally
http.Free();
end;
end;
procedure Button2Click(Sender: TObject);
var
http : TIdHttp;
url : string;
code : integer;
begin
url := 'http://www.WEBSITE.com';
http := TIdHTTP.Create(nil);
try
try
http.Get(url, nil);
code := http.ResponseCode;
except
on E: EIdHTTPProtocolException do
code := http.ResponseCode; // or: code := E.ErrorCode;
end;
ShowMessage(IntToStr(code));
finally
http.Free();
end;
end;
这篇关于IdHttp只需获取响应代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!