Indy不处理时获取响应文本302 [英] Indy Get response text while not handling 302

查看:179
本文介绍了Indy不处理时获取响应文本302的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    FHTTP.HandleRedirects := False;
    try

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

有重定向302,但我需要从此请求中获取文本. 响应:

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

作为回应:

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

推荐答案

HandleRedirects := False时,302状态代码将导致TIdHTTP引发EIdHTTPProtocolException异常.
响应的内容可以通过EIdHTTPProtocolException.ErrorMessage访问.

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>

这篇关于Indy不处理时获取响应文本302的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆