FHTTP.HandleRedirects := False;
try
StrPage := FHTTP.Get('https://somesite.site');
except
end;
有重定向 302 ,但我需要从这个请求中获取文本.. 回应:
(Status-Line):HTTP/1.1 302 Found Cache-Control:no-cache, no-store Content-Length:148291 Content-Type:text/html; charset=utf-8 Date:Sun, 21 Sep 2014 09:13:49 GMT Expires:-1 Location:/di Pragma:no-cache
In response :
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="/di">here</a>.</h2>
</body></html>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
...
我怎么收不到这段文字?
最佳答案
当 HandleRedirects := False
时,302 状态代码将导致 TIdHTTP
引发 EIdHTTPProtocolException
异常。
可以通过 EIdHTTPProtocolException.ErrorMessage
访问响应的内容。
示例:
procedure TForm1.Button1Click(Sender: TObject);
var
StrPage: string;
begin
IdHttp1.HandleRedirects := False;
try
StrPage := IdHttp1.Get('http://www.gmail.com/');
except
on E: EIdHTTPProtocolException do
begin
if not ((E.ErrorCode = 301) or (E.ErrorCode = 302)) then raise;
StrPage := E.ErrorMessage;
ShowMessage(StrPage);
end
else
raise;
end;
end;
输出:
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://mail.google.com/mail/">here</A>.
</BODY></HTML>
关于delphi - Indy 在不处理 302 的情况下获取响应文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25957871/