Indy Get response text while not handling 302

强颜欢笑 提交于 2019-12-11 12:32:19

问题


    FHTTP.HandleRedirects := False;
    try

      StrPage := FHTTP.Get('https://somesite.site');
    except
    end;

There is redirect 302 , but i need to get text from this reqest.. Response:

    (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">
...

How cant i get this text?


回答1:


When HandleRedirects := False, 302 status code will cause TIdHTTP to raise an EIdHTTPProtocolException exception.
The content of the response can be accessed via the EIdHTTPProtocolException.ErrorMessage.

Example:

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;

Output:

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



回答2:


In addition to @kobik's answer (which is technically accurate), there are some additional considerations.

The response body text you showed is fairly minimal, the only real piece of useful information it provides is a human-readable text message that includes an HTML link to the URL being redirected to. If you are just interested in the URL, you can obtain it by itself from the TIdHTTP.Response.Location property, or from the TIdHTTP.OnRedirect event. In the case of OnRedirect, you can set its Handled parameter to False to skip the actual redirection without having to set HandleRedirets to False.

If you do not want an EIdHTTPProtocolException exception being raised on 302, you can either enable the hoNoProtocolErrorException flag in the TIdHTTP.HTTPOptions property, or else call TIdHTTP.DoRequest() directly and specify 302 in its AIgnoreReplies property. Either way, you can then check the TIdHTTP.Response.ResponseCode property after the request exits without raising an exception. However, if you disable EIdHTTPProtocolException, you lose access to the body text (TIdHTTP will read and discard it) unless you enable the hoWantProtocolErrorContent flag. Either way, you will have access to the response headers.



来源:https://stackoverflow.com/questions/25957871/indy-get-response-text-while-not-handling-302

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!